使用AFNetworlk上传多张图片
2015-07-13 10:19
363 查看
-(void)uploadPictureFirst:(NSArray*)imgArray{
if ([imgArray count]>0) {
NSMutableDictionary*param=[NSMutableDictionary dictionary];
[param setValue:[NSNumber numberWithInteger:UPLOAD_FILE_TYPE_CAR] forKey:@"UploadPicType"];
NSString*urlStr=[NSString stringWithFormat:@"%@%@",BASE_API_URL,UPLOAD_PICTURE_INFO_API_URL];
NSMutableArray *mutableOperations = [NSMutableArray array];
for (UIImage*img in imgArray) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int random1=[util getOneRandomInteger];
int random2=[util getOneRandomInteger];
NSString*fileName=[NSString stringWithFormat:@"%d%d.jpg",random1,random2];
NSData*data=UIImageJPEGRepresentation(img, 0.5);
[formData appendPartWithFileData:data name:UPLOAD_FILE_NAME_KEY fileName:fileName mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {
//以下是处理返回结果
for(AFHTTPRequestOperation*operation in operations){
NSDictionary*dict=[NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
NSArray*temp=[dict valueForKey:@"results"];
if (temp!=nil&&[temp count]>0) {
[imgPathArray addObject:[temp objectAtIndex:0]];
}
}
[self realSubmit];
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
}
}
if ([imgArray count]>0) {
NSMutableDictionary*param=[NSMutableDictionary dictionary];
[param setValue:[NSNumber numberWithInteger:UPLOAD_FILE_TYPE_CAR] forKey:@"UploadPicType"];
NSString*urlStr=[NSString stringWithFormat:@"%@%@",BASE_API_URL,UPLOAD_PICTURE_INFO_API_URL];
NSMutableArray *mutableOperations = [NSMutableArray array];
for (UIImage*img in imgArray) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int random1=[util getOneRandomInteger];
int random2=[util getOneRandomInteger];
NSString*fileName=[NSString stringWithFormat:@"%d%d.jpg",random1,random2];
NSData*data=UIImageJPEGRepresentation(img, 0.5);
[formData appendPartWithFileData:data name:UPLOAD_FILE_NAME_KEY fileName:fileName mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {
//以下是处理返回结果
for(AFHTTPRequestOperation*operation in operations){
NSDictionary*dict=[NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
NSArray*temp=[dict valueForKey:@"results"];
if (temp!=nil&&[temp count]>0) {
[imgPathArray addObject:[temp objectAtIndex:0]];
}
}
[self realSubmit];
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
}
}
相关文章推荐
- 火车时刻表WebApp
- ASP.NET MVC程序中动态修改form的Action值
- IOS 本地通知
- 悟空学Linux专栏----第23篇
- Linux Resin 安装
- C#笔记 泛型 Generic
- Linux grep 命令
- 利用套打和分栏巧妙来做商品价签
- Git bash 操作记住密码
- PAT 数据结构 02-线性结构2. 一元多项式求导 (25)
- Sencha(Extjs) Ext.data.Store与Reader结合加载多重嵌套数据对象
- iOS开发中那些高效常用的宏
- 每天一个linux命令(49):at命令
- HDU 1003:Max Sum
- 利用套打和分栏巧妙来做商品价签
- HDU 1003:Max Sum
- Linux下导入、导出MySQL数据库
- 【初級篇】华为NAT技术(静态NAT)
- ubuntu jdk1.7 安装
- POJ 3276 Face The Right Way(反转)