</pre><pre code_snippet_id="1815940" snippet_file_name="blog_20160808_1_3554133" name="code" class="objc"><pre name="code" class="objc"> //判斷當前應用是否能訪問相冊資源
/*
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用戶尚未做出了選擇這個應用程序的問候
ALAuthorizationStatusRestricted, 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限。
ALAuthorizationStatusDenied, 用戶已經明確否認了這一照片數據的應用程序訪問.
ALAuthorizationStatusAuthorized 用戶已授權應用訪問照片數據.
}
*/
<pre name="code" class="objc">首先導入 AssetsLibrary.framework AVFoundation庫 導入頭文件
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
/**
* 調用系統相機
*/
- (void)callCamera
{
//判斷是否已授權
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == ALAuthorizationStatusDenied||authStatus == ALAuthorizationStatusRestricted) {
[self setAlertControllerWithTitle:@"提示" message:@"請前往設置->隱私->相機授權應用拍照權限" actionTitle:@"確定"];
return ;
}
}
// 判斷是否可以打開相機
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
} else {
[self setAlertControllerWithTitle:@"提示" message:@"你沒有相機" actionTitle:@"確定"];
}
}
/**
* 調用系統相冊
*/
- (void)callPhoto
{
//判斷是否已授權
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
if (authStatus == ALAuthorizationStatusDenied) {
[self setAlertControllerWithTitle:@"提示" message:@"請前往設置->隱私->相冊授權應用訪問相冊權限" actionTitle:@"確定"];
return;
}
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//self.isReload = NO;
[self presentViewController:picker animated:YES completion:nil];
} else {
[self setAlertControllerWithTitle:@"提示" message:@"你沒有相冊" actionTitle:@"確定"];
}
}