上传多张图片方法

[SVProgressHUD showWithStatus:@"正在上传图片中.........."];

//需要传递的参数,jason格式    

NSDictionary *parameter = @{@"content":textviewStr};    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];    

manager.responseSerializer = [AFHTTPResponseSerializer serializer];    

[manager POST:[NSString stringWithFormat:@"%@/?num=1023",HostUrl] parameters:parameter constructingBodyWithBlock:^(idformData) {



for (int i = 0;i < _array.count; i++) {

UIImage *himages = [_array objectAtIndex:i];

//将图片转化成data

NSData *imageData;

if (UIImagePNGRepresentation(himages)) {

imageData = UIImageJPEGRepresentation(himages, 0.5);

} else {

imageData = UIImagePNGRepresentation(himages);

}

// 上传的参数名

NSString * Name = [NSString stringWithFormat:@"pic%d", i+1];

// 上传filename

NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];

//                [photoNameAry addObject:fileName];

[formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"jpg/jpeg"];

}

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

//HStop(self);

NSLog(@"上传成功 %@", responseObject);

NSString *jsonStr = [NSString stringWithFormat:@"%@",responseObject];

if (jsonStr.length==0) {

} else {

NSData* jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

//解析json数据,使用系统方法 JSONObjectWithData:  options: error:

NSDictionary *dddic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"praise_json==========%@",dddic);

NSString *msgStr = [NSString stringWithFormat:@"%@",dddic[@"msg"]];

NSLog(@"msgStr=======%@",msgStr);

//[HTools showTextOnlyHud:msgStr delay:1.0];

//[SVProgressHUD dismissWithDelay:1.0];

[SVProgressHUD showSuccessWithStatus:@"图片上传成功......."];

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//HStop(self);

//[SVProgressHUD dismissWithDelay:1.0];

[SVProgressHUD showSuccessWithStatus:@"图片上传失败......."];

NSLog(@"error=============%@",error);

}];

你可能感兴趣的:(上传多张图片方法)