一.AFNetworking上传图片
/**
需要Demo,可留言
*/
1.利用AFnetworking上传图片.
2.注意将UIImage类型转化为NSData类型,传给后台.
二.Demo
// // ViewController.m // Demo-AFNetworking-上传图片 // // Created by quhaola on 16/4/8. // Copyright © 2016年 MC. All rights reserved. // #import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> #import "AFHTTPRequestOperationManager.h" #define YYIP @"http://服务器地址" @interface ViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController #pragma mark - 生命周期 - (void)viewDidLoad { [super viewDidLoad]; //从相册选择 [self addLibraryButton]; //从拍照选取 [self addPhotoButton]; //展示选择或者拍照的图片 [self addImageView]; } #pragma mark - 实现方法 #pragma mark 相册按钮 - (void)addLibraryButton { UIButton * libraryButton = [UIButton buttonWithType:UIButtonTypeCustom]; libraryButton.frame = CGRectMake(50, 30, 200, 40); libraryButton.backgroundColor = [UIColor blueColor]; [libraryButton setTitle:@"相册" forState:UIControlStateNormal]; [libraryButton addTarget:self action:@selector(addLibraryButtonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:libraryButton]; } #pragma mark 拍照按钮 - (void)addPhotoButton { UIButton * photoButton = [UIButton buttonWithType:UIButtonTypeCustom]; photoButton.frame = CGRectMake(50, 80, 200, 40); photoButton.backgroundColor = [UIColor blueColor]; [photoButton setTitle:@"拍照" forState:UIControlStateNormal]; [photoButton addTarget:self action:@selector(photoButtonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:photoButton]; } #pragma mark 展示图片 - (void)addImageView { self.imageView = [[UIImageView alloc] init]; self.imageView.frame = CGRectMake(10, 150, self.view.bounds.size.width - 20, 300); self.imageView.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.imageView]; } #pragma mark - 点击事件 #pragma mark 相册按钮的点击事件 - (void)addLibraryButtonClicked { NSLog(@"相册"); //先判断是否支持相册类型 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { NSLog(@"相册不可用"); return; } //创建图片选取类 UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:imagePickerController animated:YES completion:^{ }]; } #pragma mark 拍照按钮的点击事件 - (void)photoButtonClicked { NSLog(@"拍照"); if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"相机不可用"); return; } NSArray * availableMeidatypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; BOOL canTakePicture = NO; for (NSString * mediaType in availableMeidatypes) { if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { //支持拍照 canTakePicture = YES; break; } } if (!canTakePicture) { NSLog(@"不支持拍照"); return; } //创建图片选取控制器 UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil]; imagePickerController.allowsEditing = YES; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:^{ }]; } #pragma mark - UIimagePickerController的代理方法 #pragma mark 确认按钮 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { //获取媒体类型 NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType]; //判断是静态还是视频 //如果是图片 if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { //获取用户编辑之后的图像 UIImage * editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; [self sendNetWorking_iconWithImage:(UIImage *)editedImage]; //将图像保存到媒体库中 UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); self.imageView.image = editedImage; [self dismissViewControllerAnimated:YES completion:^{ }]; } } #pragma mark 取消按钮 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { } #pragma mark 将该图像保存到媒体库中调用的方法 - (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (!error) { NSLog(@"保存成功"); } else { NSLog(@"保存失败"); } }
#pragma mark 发起网络请求
- (void)sendNetWorking_iconWithImage:(UIImage *)image
{ NSString * urlStr = [NSString stringWithFormat:@"%@%@",YYIP,@"changeAvatar"]; //除了文件 NSDictionary * parameter = @{ @"id" : @"445" }; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlStr parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { /** 参数: 1. data:<#(nonnull NSData *)#> 获取的UIImage对象,转化为NSData类型 2. name:<#(nonnull NSString *)#> 与后台规定的名字一致 3. fileName:<#(nonnull NSString *)#> 文件的名字,必须不一致,可以用当前时间作为文件名 4. mineType:<#(nonnull NSString *)#> 文件类型 */ NSData *imageData = UIImageJPEGRepresentation(image, 0.3); [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"image.png" mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"responseObject: %@",responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@",error); }]; } @end