- (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:^{ }];}
提交单张图片 :
// 向服务器提交图片
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);
}];
提交多张图片:
// 向服务器提交图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 显示进度
[manager POST:urlstr parameters:[self Params] constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 上传 多张图片
for(NSInteger i = 0; i < self.imageDataArray.count; i++) {
NSData * imageData = [self.imageDataArray objectAtIndex: i];
// 上传的参数名
NSString * Name = [NSString stringWithFormat:@"%@%zi", Image_Name, i+1];
// 上传filename
NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
[formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"完成 %@", result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"错误 %@", error.localizedDescription);
}];