需要实现的协议
@interface RegistController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
1 - (void)postHeadImage{ 2 //打开 图片的用户交互 3 self.headimageView.userInteractionEnabled = YES; 4 5 //创建一个点击手势 6 UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap_View:)]; 7 [self.headimageView addGestureRecognizer:tapGR]; 8 } 9 //点击手势的事件 10 - (void)tap_View:(UITapGestureRecognizer *)tapGR{ 11 //点击图片后, 调用相册, 选取图中的图片, 12 //[说明]照片的获取有两种方式, 1) 调用摄像头,模拟器不能使用摄像头,需要真机 2)第二种方式是调用相册获取图片 13 //先判断摄像头是否可用, 14 // if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 15 // //摄像头可用 16 // [self loadImageWithType:UIImagePickerControllerSourceTypeCamera]; 17 // }else{ 18 // //摄像头不可用 19 // 20 // } 21 22 23 //调用相册 24 [self loadImageWithType:UIImagePickerControllerSourceTypePhotoLibrary]; 25 26 } 27 28 //调用摄像头或相册,调用此函数 29 - (void)loadImageWithType:(UIImagePickerControllerSourceType)imageType{ 30 /**数据来源枚举 31 32 UIImagePickerControllerSourceTypePhotoLibrary, 相册 33 UIImagePickerControllerSourceTypeCamera, 照相机 34 UIImagePickerControllerSourceTypeSavedPhotosAlbum 调用已经保存的文件(如: 图片, 视频均可) 35 36 */ 37 38 //1. 创建图片容器 39 UIImagePickerController * picker = [[UIImagePickerController alloc]init]; 40 //2. 设置操作类型 41 picker.sourceType = imageType; 42 //3. 给图片容器签订协议<UIImagePickerControllerDelegate,UINavigationControllerDelegate> 43 picker.delegate = self; 44 45 //4. 显示图片容器的视图 46 [self presentViewController:picker animated:NO completion:nil]; 47 } 48 49 //协议回调函数, 此函数在相册中或摄像头中选择一张照片时, 被回调 50 /** 51 * @param picker 当前的图片容器 52 * @param info 选取的图片信息 53 */ 54 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 55 //从参数中获取选取的图片 56 UIImage * image = info[UIImagePickerControllerOriginalImage]; 57 //把图片显示到页面控件中 58 self.headimageView.image = image; 59 60 //上传图片到服务器 61 //1. 图片转换成NSData 62 NSData * data = UIImagePNGRepresentation(image); 63 //2. post方式上传data 64 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; 65 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 66 67 //上传文本数据 68 // [manager POST:@"http://10.0.8.8/sns/my/upload_headimage.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 69 // //网络请求成功回调块 70 // 71 // } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 72 // //网络请求失败回调块 73 // 74 // }]; 75 76 [manager POST:@"XXXXXXXX" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 77 //指定上传图片(NSData) 上传文本之外的数据 78 //参数1: 要上传得附件 79 //参数2: 上传文件的名称, 与后台key值一致 80 //参数3: 所上传的附件的名字(应该保证上传的文件名不重复: 策略: 年月日时分秒毫秒3到5位随机数) 81 //参数4: 上传附件的扩展名 82 [formData appendPartWithFileData:data name:@"headimage" fileName:@"1.png" mimeType:@"image/png"]; 83 84 } success:^(AFHTTPRequestOperation *operation, id responseObject) { 85 //网络请求成功回调块 86 87 NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"message"]); 88 89 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 90 //网络请求失败回调块 91 NSLog(@"%@", error); 92 }]; 93 94 [picker dismissViewControllerAnimated:YES completion:nil]; 95 }
下载文件
1 - (IBAction)btn_Download:(id)sender { 2 // 完成图片的下载功能 3 //1. 创建下载管理类对象, 初始化配置文件 4 AFURLSessionManager * manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 5 6 //2. 确定下载地址 7 NSString * path = @"http://c.hiphotos.baidu.com/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=38c40c12718da9775a228e79d138937c/96dda144ad345982c0e32ab30ef431adcbef84a2.jpg"; 8 //3. 提交请求时, 应该擦除掉地址中的非法字符, 使用utf-8 编码格式 9 //NSString * urlString = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 10 //4. 设置请求对象 11 NSURL * url = [NSURL URLWithString:path]; 12 NSURLRequest * request = [NSURLRequest requestWithURL:url]; 13 14 //5. 发送请求进行下载 15 NSURLSessionDownloadTask * task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 16 //告知下载后文件的存在路径 17 //NSHomeDirectory() 沙盒路径, 手机运行程序, 或程序读取的空间 18 NSString * filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/123.png"]; 19 NSLog(@"%@", filePath); 20 return [NSURL fileURLWithPath:filePath]; 21 22 } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nonnull filePath, NSError * _Nonnull error) { 23 //判断下载是否出错 24 if (error) { 25 NSLog(@"%@", error); 26 }else{ 27 NSLog(@"下载成功, 下载路径:%@", filePath); 28 } 29 30 }]; 31 32 //6. 启动下载 33 [task resume]; 34 35 }