前言
照片的選取在App中是相當(dāng)普遍的場景苍凛,如用戶修改頭像询枚、上傳照片。一般給用戶兩種選擇
1拍照
2從相冊選取
在iOS中選擇圖片這個任務(wù)是通過UIImagePickerController來實(shí)現(xiàn)的呻疹,通過設(shè)置sourceType來決定是從相冊選取還是拍照话侧,當(dāng)前的視圖控制器需要實(shí)現(xiàn)UIImagePickerControllerDelegate協(xié)議的方法。
imagePickerController:didFinishPickingMediaWithInfo:
//用戶選擇了圖片
imagePickerControllerDidCancel:
//用戶取消選擇
由于iOS系統(tǒng)的隱私保護(hù)機(jī)制益老,無論那種方式都必須先由用戶授權(quán)否則我們沒有權(quán)限彪蓬,結(jié)果如下圖
1,我們希望這個體驗進(jìn)行優(yōu)化捺萌,2档冬,這個場景很多,我們希望將其模塊化,以便復(fù)用和拆封ViewController桃纯。我們這里只討論選擇一張圖片的情況酷誓,多選照片的類庫代碼,現(xiàn)成類庫比較多态坦。
實(shí)現(xiàn)
綜合來說我們的流程如下:1.彈出視圖供用戶選擇一種方式盐数;2.確定App是否有相應(yīng)的權(quán)限,如果沒有驮配,提示用戶沒有權(quán)限并嘗試引導(dǎo)用戶去設(shè)置里面進(jìn)行授權(quán)娘扩;3.選擇照片返回着茸。注意:UIActionSheet和UIAlertView在iOS9中已經(jīng)廢棄壮锻,提倡使用UIAlertController′汤考慮后期的復(fù)用猜绣、維護(hù)與完善,我們將其進(jìn)行封裝先定義一個ImagePickerModelDelegate協(xié)議敬特,用來和視圖控制器進(jìn)行交互掰邢。
@protocol ImagePickerModelDelegate
-(void)imagePickerDidFinishWithImage:(UIImage)image;
-(void)imagePickerDidCancel;
//彈出警告框的視圖
-(UIView)viewControllerView;
@end
新建NSObject的子類ImagePickerModel,并實(shí)現(xiàn)UIImagePickerControllerDelegate和UIActionSheetDelegate協(xié)議伟阔。聲明兩個方法
+(id)sharedInstance;
-(void)startShowSelectTypeViewWithViewController:(id)viewController andIsEdit:(BOOL)edit;
.m文件方法的實(shí)現(xiàn)
+(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;
}
}```
無論那種操作之前辣之,都先判斷下權(quán)限
//從相冊選取
- (void)takePicFromAlbum
{
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == 1 || author == 2)
{
if(isiOS8)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"沒有相冊訪問權(quán)限,請在設(shè)置-隱私-相冊中進(jìn)行設(shè)置皱炉!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"設(shè)置",nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"沒有相冊訪問權(quán)限怀估,請在設(shè)置-隱私-相冊中進(jìn)行設(shè)置!" 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];
}
//從相機(jī) - (void)takePicFromCamera
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
if(isiOS8)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"沒有相機(jī)訪問權(quán)限,請在設(shè)置-隱私-相機(jī)中進(jìn)行設(shè)置多搀!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"設(shè)置",nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"沒有相機(jī)訪問權(quán)限歧蕉,請在設(shè)置-隱私-相機(jī)中進(jìn)行設(shè)置!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"設(shè)置",nil];
}
[alertView show];
return;
//無權(quán)限
}
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(@"沒有相機(jī)權(quán)限康铭!");
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//iOS8后允許打開系統(tǒng)設(shè)置
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];
}