iOS 调取本地相册/相机,剪裁图片进行头像上传
2015-09-15 16:30
696 查看
打开本地相册或打开本地相机拍照,获取图片裁剪图片,上传图片
加入头文件#import <MobileCoreServices/MobileCoreServices.h> #import <***Foundation/***Foundation.h> #import <AssetsLibrary/AssetsLibrary.h>
全部变量
NSString*THE_IMG_PATH; NSString *_imageString
添加代理
<VPImageCropperDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
相关代码
#pragma mark 用户头像点击响应的方法, - (void)getImageFrom{ UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选取", nil]; [choiceSheet showInView:self.view]; } #pragma mark UIActionSheetDelegate 点击ActionSheet 选择头像来源相册或者相机 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { // 来源相机-拍照 NSString *mediaType = ***MediaTypeVideo; ***AuthorizationStatus authStatus = [***CaptureDevice authorizationStatusForMediaType:mediaType]; if(authStatus == ALAuthorizationStatusRestricted ||authStatus == ALAuthorizationStatusDenied){ //不能访问相机 NSLog(@"相机权限受限"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相机失败" message:@"请打开 设置-隐私-相机 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; return; }else{ //设置可以访问相机 if ([UIImagePickerOrCamera isCameraAvailable] && [UIImagePickerOrCamera doesCameraSupportTakingPhotos]) { UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerOrCamera isFrontCameraAvailable]) { controller.cameraDevice =UIImagePickerControllerCameraDeviceFront; } NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; controller.mediaTypes = mediaTypes; controller.delegate = self; [self presentViewController:controller animated:YES completion:^(void){ NSLog(@"Picker View Controller is presented"); }]; } } } else if (buttonIndex == 1) { //来源相册——访问相册 ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus]; if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied) { //设置不能访问相册 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相册失败" message:@"请打开 设置-隐私-照片 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }else{ //设置可以访问相册 if ([UIImagePickerOrCamera isPhotoLibraryAvailable]) { UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; controller.mediaTypes = mediaTypes; controller.delegate = self; [self presentViewController:controller animated:YES completion:^(void){ NSLog(@"Picker View Controller is presented"); }]; } } } }
#pragma mark - 相册导航器的 cancle的响应,退出相册 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:^(){ //TO Do }]; } //对相册的导航条修改 #pragma mark - UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; if ([navigationController isKindOfClass:[UIImagePickerController class]] &&((UIImagePickerController *)navigationController).sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) { [[UIApplication sharedApplication] setStatusBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO]; } } #pragma mark 设置相册导航控制器的 导航模式 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { - [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; }
//裁剪 #pragma mark UIImagePickerControllerDelegate 裁剪图片 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //相册退出 [picker dismissViewControllerAnimated:YES completion:^() { UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; portraitImg = [UIImagePickerOrCamera imageByScalingToMaxSize:portraitImg]; // 裁剪 VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0]; imgEditorVC.delegate = self; [self presentViewController:imgEditorVC animated:YES completion:^{ // TO DO }]; }]; } #pragma mark 取消裁剪 - (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController { [cropperViewController dismissViewControllerAnimated:YES completion:^{ }]; } #pragma mark VPImageCropperDelegate 裁剪完成,对图片压缩处理 - (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage { //存储在本地 THE_IMG_PATH=[UIImagePickerOrCamera saveImage:editedImage WithName:@"userHeadImage.png"]; //上传小图片,对图片尺寸压缩 editedImage=[UIImagePickerOrCamera imageWithImage:editedImage scaledToSize:CGSizeMake(Main_Screen_Width, Main_Screen_Width)]; //图片Gzip压缩 NSData*imageData=UIImagePNGRepresentation(editedImage); //可以对图片进行gzip压缩或者其他相应处理 _imageString=[[NSString alloc]initWithData:imageData encoding:NSUTF8StringEncoding]; /** */ [cropperViewController dismissViewControllerAnimated:YES completion:^{ // TO DO /** 图片裁剪完成,可以做相应处理 */ }]; }
图片的上传 这里是用第三方AFNetWorking
-(viod)updata{ UIApplication *application = [UIApplication sharedApplication]; AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager]; manager.requestSerializer.timeoutInterval = 5; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/javascript",@"text/html", @"application/json",nil]; NSArray*arrar=[THE_IMG_PATH componentsSeparatedByString:@"/"]; NSString*imageName=[arrar objectAtIndex:arrar.count-1]; NSMutableDictionary*dic=@{@"image":_imageString,@"imagePath":imageName}; [manager POST:urlString parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) { if(sucess){ sucess(responseObject); NSLog(@"responseObject=%@",responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"LYYB----AFNetWoring.errno=%@",error); fails(error); }]; }
相关文章推荐
- iOS获取plist文件属性值的方法
- iOS开发之 手势
- iOS 不要使用tag传递TableViewCell的indexPath值
- ios 定位 航向检测
- SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件
- SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件
- iOS 定位精度
- ios socket第三方框架 AsyncSocket使用简介,连接,心跳,断线,数据发送与接收
- iOS学习资源搜集
- IOS开发+(void)load与+(void)initialize区别
- Crashlytics——iOS crash log 解析利器
- ios 线程安全单例写法
- 【投稿】iOS蓝牙开发(一)蓝牙相关基础知识
- iOS runtime方法详解之添加类
- IOS贝塞尔曲线圆形进度条和加载动画
- ios 关于scrollview内空间位置与想象不符的解决办法
- iOS设置分割线从边框顶端开始
- ios 读取图片
- VMware虚拟机 停电 无法启动
- iOS开发的一些奇巧淫技