使用AFNetworlk上传多张图片
2018-07-20 来源:open-open
-(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]; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐