iOS实现压缩图片上传功能
本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下
#pragmamark-打开相机 -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary<NSString*,id>*)info{ UIImage*image=info[UIImagePickerControllerOriginalImage]; self.currentTapCell.photoManageImgView.image=image; NSIndexPath*indexPath=[self.baseTableindexPathForCell:self.currentTapCell]; NSString*key=[NSStringstringWithFormat:@"ineed%u%u",indexPath.row,indexPath.section]; [[NetEngineHelpershareNetEngine]postUploadWithUrl:k_uploadPhoto_urlimage:imagefileName:[NSStringstringWithFormat:@"%@.png",key]fileType:@"PNG/JPEG/JPG"success:^(idresponse){ if([response[@"STATUS"]intValue]!=0){ NSString*str=@""; if(response[@"ERRORDESC"]){ str=response[@"ERRORDESC"]; }else{ str=@"系统异常"; } [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=str; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; }else{ [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=@"上传成功"; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; if([self.currentTapCell.photoDescribeLable.textisEqualToString:@"上传诊断证明"]){ [self.successUploadDicsetValue:response[@"FILEID"]forKey:@"PROVEID"]; } if([self.currentTapCell.photoDescribeLable.textisEqualToString:@"上传病历首页"]){ [self.successUploadDicsetValue:response[@"FILEID"]forKey:@"CASEHOMEID"]; } NSLog(@"上传结果%@",response); } }fail:^(NSError*error){ [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=error.userInfo[@"NSLocalizedDescription"]; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; }]; [selfdismissViewControllerAnimated:YEScompletion:^{ }]; } #pragmamark-打开相机 -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary<NSString*,id>*)info{ UIImage*image=info[UIImagePickerControllerOriginalImage]; self.currentTapCell.photoManageImgView.image=image; NSIndexPath*indexPath=[self.baseTableindexPathForCell:self.currentTapCell]; NSString*key=[NSStringstringWithFormat:@"ineed%u%u",indexPath.row,indexPath.section]; [[NetEngineHelpershareNetEngine]postUploadWithUrl:k_uploadPhoto_urlimage:imagefileName:[NSStringstringWithFormat:@"%@.png",key]fileType:@"PNG/JPEG/JPG"success:^(idresponse){ if([response[@"STATUS"]intValue]!=0){ NSString*str=@""; if(response[@"ERRORDESC"]){ str=response[@"ERRORDESC"]; }else{ str=@"系统异常"; } [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=str; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; }else{ [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=@"上传成功"; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; if([self.currentTapCell.photoDescribeLable.textisEqualToString:@"上传诊断证明"]){ [self.successUploadDicsetValue:response[@"FILEID"]forKey:@"PROVEID"]; } if([self.currentTapCell.photoDescribeLable.textisEqualToString:@"上传病历首页"]){ [self.successUploadDicsetValue:response[@"FILEID"]forKey:@"CASEHOMEID"]; } NSLog(@"上传结果%@",response); } }fail:^(NSError*error){ [AlertHelpershareAlertHelper].onVC=self; [AlertHelpershareAlertHelper].alertTitle=error.userInfo[@"NSLocalizedDescription"]; [AlertHelpershareAlertHelper].alertMessage=nil; [[AlertHelpershareAlertHelper]alertVcWithSureAction:^(idresponse){ }]; }]; [selfdismissViewControllerAnimated:YEScompletion:^{ }]; } /**上传文件*/ -(void)postUploadWithUrl:(NSString*)urlStrimage:(UIImage*)imagefileName:(NSString*)fileNamefileType:(NSString*)fileTyesuccess:(Success)successfail:(Faile)fail{ NSString*resultStr=[urlStrstringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]]; self.success=success; self.failer=fail; AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManagermanager]; manager.responseSerializer=[AFHTTPResponseSerializerserializer]; manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObjects:@"text/html",@"application/json",@"text/plain",nil]; [manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Content-Type"]; manager.responseSerializer=[AFJSONResponseSerializerserializer]; manager.requestSerializer=[AFHTTPRequestSerializerserializer]; [manager.securityPolicysetAllowInvalidCertificates:YES]; UIApplication*application=[UIApplicationsharedApplication]; application.networkActivityIndicatorVisible=YES; [managerPOST:resultStrparameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>formData){ NSData*imgData=UIImageJPEGRepresentation(image,0.02); [formDataappendPartWithFileData:imgDataname:@"FILENAME"fileName:fileNamemimeType:fileTye]; }success:^(AFHTTPRequestOperation*operation,idresponseObject){ success(responseObject); application.networkActivityIndicatorVisible=NO; }failure:^(AFHTTPRequestOperation*operation,NSError*error){ fail(error); application.networkActivityIndicatorVisible=NO; }]; }
以上就是本文的全部内容,希望对大家学习iOS程序设计有所帮助。