iOS 使用AFN 进行单图和多图上传

来源:互联网 发布:淘宝店更换旺旺 编辑:程序博客网 时间:2024/06/01 09:35
图片上传时必要将图片进行压缩,不然会上传失败
1.单张图上传
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];   
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
//使用日期生成图片名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:fileName mimeType:@"image/png"];
}success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
//上传图片成功执行回调
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//上传图片失败执行回调
completion(nil,error);
}];
2.多图上传多图上传和单图上传区别在于文件名称
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {NSInteger imgCount = 0;for (NSData *imageData in imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(imgCount)];
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"uploadFile%@",@(imgCount)] fileName:fileName mimeType:@"image/png"];imgCount++;
}} success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {completion(nil,error);
}];

文/Spykerking(简书作者)
原文链接:http://www.jianshu.com/p/0e28fdef0f91


图片压缩方法

UIImageJPEGRepresentation方法在耗时上比较少 而UIImagePNGRepresentation耗时操作时间比较长

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

使用UIImagePNGRepresentation取得照片时候可能会造成卡顿的现象

在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. 


UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小.


[cpp] view plain copy
print?
  1. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  2.  [formatter setDateFormat:@"YYYY-MM-DD-hh-mm-ss"];  

[cpp] view plain copy
print?
  1. if (UIImagePNGRepresentation(image)==nil) {  
  2.     data = UIImageJPEGRepresentation(image, 1.0);  
  3. }else{  
  4.     data = UIImagePNGRepresentation(image);  
  5. }  


0 0