iOS 调取本地相册/相机,剪裁图片进行头像上传

打开本地相册或打开本地相机拍照,获取图片裁剪图片,上传图片
加入头文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.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 = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice 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);
    }];
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值