iOS图片选择的优化与封装

7 篇文章 0 订阅
2 篇文章 0 订阅

前言

照片的选取在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 *)actionSheet
clickedButtonAtIndex:(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 ([UIImagePickerController
isSourceTypeAvailable: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 *)picker
didFinishPickingMediaWithInfo:(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值