ios 上传多张图片总结——IOS网络访问之使用AFNetworking
Posted Larry_qidian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 上传多张图片总结——IOS网络访问之使用AFNetworking相关的知识,希望对你有一定的参考价值。
ios 上传图片文件 或者 视频
AFHTTPRequestOperationManager
上传图片(多张)参考文献:
http://www.blogjava.net/qileilove/archive/2014/12/11/421323.html
下面两个函数是可以用的:
[objc] view plain copy- [formData appendPartWithFileData:<#(NSData *)#> name:<#(NSString *)#> fileName:<#(NSString *)#> mimeType:<#(NSString *)#>]
- [formData appendPartWithFileURL:<#(NSURL *)#> name:<#(NSString *)#> error:<#(NSError *__autoreleasing *)#>]
- [formData appendPartWithFileURL:<#(NSURL *)#> name:<#(NSString *)#> fileName:<#(NSString *)#> mimeType:<#(NSString *)#> error:<#(NSError *__autoreleasing *)#>]
这个函数,我用时失败:
[formData appendPartWithFormData:<#(NSData *)#> name:<#(NSString *)#>]
保存图像:
[objc] view plain copy- - (void)viewDidLoad
- [super viewDidLoad];
- // 保存每次添加的图片
- self.imageDataArray = [NSMutableArray array];
- -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- UIImage *originalImage = [info valueForKey:UIImagePickerControllerEditedImage] ;
- // 得到图片的缓存数据
- NSData * imagedata = UIImageJPEGRepresentation([originalImage imageByScalingAndCroppingForSize:CGSizeMake(originalImage.size.width, originalImage.size.height)], 0.5);
- static int index = 1;
- NSString * newImageName = [NSString stringWithFormat:@"%@%zi%@", Image_Name, index, @".jpg"];
- NSString *jpgPath = NSHomeDirectory();
- jpgPath = [jpgPath stringByAppendingPathComponent:@"Documents"];
- jpgPath = [jpgPath stringByAppendingPathComponent:newImageName];
- [imagedata writeToFile:jpgPath atomically:YES];
- [self.imageDataArray addObject:imagedata];
- index ++ ;
- // 显示当前上传图片
- [self showUploadImage:imagedata];
- [picker dismissViewControllerAnimated:YES completion:^
- ];
提交单张图片 : [objc] view plain copy
- // 向服务器提交图片
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- // 显示进度
- [manager POST:urlstr parameters:[self Params] constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
- static int nindex = 1;
- // 单张图片上传
- NSString * paramName = [NSString stringWithFormat:@"%@%zi", Image_Name, nindex];
- NSString * newImageName = [NSString stringWithFormat:@"%@.jpg", paramName];
- NSString * imagepath = NSHomeDirectory();
- NSString * path = [imagepath stringByAppendingPathComponent:@"Documents"];
- NSBundle * Bundle = [NSBundle bundleWithPath:path];
- NSURL * fileURL = [Bundle URLForResource:newImageName withExtension:nil];
- [formData appendPartWithFileURL:fileURL name:paramName error:nil];
- success:^(AFHTTPRequestOperation *operation, id responseObject)
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
- NSLog(@"完成 %@", result);
- failure:^(AFHTTPRequestOperation *operation, NSError *error)
- NSLog(@"错误 %@", error.localizedDescription);
- ];
提交多张图片:
[objc] view plain copy- // 向服务器提交图片
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- // 显示进度
- [manager POST:urlstr parameters:[self Params] constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
- iOS 一次上传多张图片, 并返回进度值