| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

使用AFNetworlk上传多张图片

  -(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];      }  }