iOS图片选择的优化与封装

来源:互联网 发布:美国失业金数据公布 编辑:程序博客网 时间:2024/05/16 05:21

前言

照片的选取在App中是相当普遍的场景,如用户修改头像、上传照片。一般给用户两种选择

1拍照
2从相册选取

在iOS中选择图片这个任务是通过UIImagePickerController来实现的,通过设置sourceType来决定是从相册选取还是拍照,当前的视图控制器需要实现UIImagePickerControllerDelegate协议的方法。

imagePickerController:didFinishPickingMediaWithInfo:
//用户选择了图片
imagePickerControllerDidCancel:
//用户取消选择

由于iOS系统的隐私保护机制,无论那种方式都必须先由用户授权否则我们没有权限,结果如下图

没有权限

1,我们希望这个体验进行优化,2,这个场景很多,我们希望将其模块化,以便复用和拆封ViewController。我们这里只讨论选择一张图片的情况,多选照片的类库代码,现成类库比较多。

实现

综合来说我们的流程如下:1.弹出视图供用户选择一种方式;2.确定App是否有相应的权限,如果没有,提示用户没有权限并尝试引导用户去设置里面进行授权;3.选择照片返回。注意:UIActionSheet和UIAlertView在iOS9中已经废弃,提倡使用UIAlertController。考虑后期的复用、维护与完善,我们将其进行封装先定义一个ImagePickerModelDelegate协议,用来和视图控制器进行交互。

@protocol ImagePickerModelDelegate
-(void)imagePickerDidFinishWithImage:(UIImage*)image;
-(void)imagePickerDidCancel;
//视图控制器的View
-(UIView*)viewControllerView;
@end

新建NSObject的子类ImagePickerModel,并实现UIImagePickerControllerDelegate和UIActionSheetDelegate协议。声明两个方法

+(id)sharedInstance;
-(void)startShowSelectTypeViewWithViewController:(id)viewController andIsEdit:(BOOL)edit;

.m文件方法的实现

+(id)sharedInstance{    if (!model)   {            model = [[ImagePickerModel alloc]init];         if([[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)      {          isiOS8 = YES;      }    }    return model;}-(void)startShowSelectTypeViewWithViewController:(id)viewController andIsEdit:(BOOL)edit{     isEdit = edit;    _delegate = viewController;    UIActionSheet *actSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];       [actSheet showInView:[_delegate viewControllerView]];       [self takePicFromAlbum];}-(void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex{     switch (buttonIndex)     {          case 0:          {               [self takePicFromAlbum];               break;          }          case 1:          {               [self takePicFromCamera];               break;          }          default:          break;     }}

无论那种操作之前,都先判断下权限

//从相册选取- (void)takePicFromAlbum{     ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];     if (author == 1 || author == 2)     {        if(isiOS8)        {           UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有相册访问权限,请在设置-隐私-相册中进行设置!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置",nil];        }       else       {           UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有相册访问权限,请在设置-隐私-相册中进行设置!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];       }          [alertView show];          return;     }     UIImagePickerController *pick = [[UIImagePickerController alloc] init];     pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     pick.delegate = self;     pick.allowsEditing = isEdit;     [(UIViewController*)_delegate presentViewController:pick animated:YES completion:nil];}//从相机- (void)takePicFromCamera{     AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];     if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)     {          if(isiOS8)          {                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有相机访问权限,请在设置-隐私-相机中进行设置!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置",nil];          }          else          {                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有相机访问权限,请在设置-隐私-相机中进行设置!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置",nil];          }       [alertView show];        return;     //无权限}     if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])     {     UIImagePickerController *pick = [[UIImagePickerController alloc] init];     pick.sourceType = UIImagePickerControllerSourceTypeCamera;     pick.delegate = self;     pick.allowsEditing = isEdit;     [(UIViewController*)_delegate presentViewController:pick animated:YES completion:nil];     }    else    {          NSLog(@"没有相机权限!");    }}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    //iOS8后允许打开系统设置    if (buttonIndex == 1)    {        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];    }}

选择或者取消后的操作

#pragma mark - UIImagePickerController- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info{    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];    if (!image)     {        image = [info objectForKey:UIImagePickerControllerOriginalImage];    }    [_delegate imagePickerDidFinishWithImage:image];    picker.delegate = nil;    [picker dismissViewControllerAnimated:YES completion:nil];}-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{    picker.delegate = nil;    [picker dismissViewControllerAnimated:YES completion:nil];    [_delegate imagePickerDidCancel];}
0 0