iOS 調(diào)用系統(tǒng)相機(jī)和相冊

首先,在Info.plist文件中添加下面兩個key,根據(jù)app具體需求設(shè)置相應(yīng)的文案:
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description

Info.plist.png

在main.storyBoard里拖一個imageView并連線,用來顯示拍攝或從相冊中選取的圖片:


main.storyBoard.png

調(diào)用系統(tǒng)相機(jī)垄开,引入頭文件:

#import <AVFoundation/AVFoundation.h>

調(diào)用系統(tǒng)相冊,引入頭文件:

#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

遵循代理,設(shè)置全局變量并初始化:

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *avatar;

@property (nonatomic, strong) UIImagePickerController *imagePickerController;

- (void)viewDidLoad {
    [super viewDidLoad];

    //為imageView添加點(diǎn)擊事件:
    self.avatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvatar)];
    [self.avatar addGestureRecognizer:tap];
}

- (UIImagePickerController *)imagePickerController {
    if (_imagePickerController == nil) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self; //delegate遵循了兩個代理
        _imagePickerController.allowsEditing = YES;
    }
    return _imagePickerController;
}

點(diǎn)擊imageView逗余,彈出系統(tǒng)彈框:

- (void)tapAvatar {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self checkCameraPermission];//檢查相機(jī)權(quán)限
    }];
    UIAlertAction *album = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         [self checkAlbumPermission];//檢查相冊權(quán)限
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

    [alert addAction:camera];
    [alert addAction:album];
    [alert addAction:cancel];

    [self presentViewController:alert animated:YES completion:nil];
}
系統(tǒng)彈框.PNG
#pragma mark - Camera
- (void)checkCameraPermission {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                [self takePhoto];
            }
        }];
    } else if (status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted) {
        [self alertCamear];
    } else {
        [self takePhoto];
    }
}

- (void)takePhoto {
    //判斷相機(jī)是否可用,防止模擬器點(diǎn)擊【相機(jī)】導(dǎo)致崩潰
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:self.imagePickerController animated:YES completion:^{

        }];
    } else {
        NSLog(@"不能使用模擬器進(jìn)行拍照");
    }
}
#pragma mark - Album
- (void)checkAlbumPermission {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (status == PHAuthorizationStatusAuthorized) {
                    [self selectAlbum];
                }
            });
        }];
    } else if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
        [self alertAlbum];
    } else {
        [self selectAlbum];
    }
}

- (void)selectAlbum {
    //判斷相冊是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePickerController animated:YES completion:^{

        }];
    }
}

- (void)alertAlbum {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請?jiān)谠O(shè)置中打開相冊" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

UIImagePickerControllerDelegate代理方法:

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    self.avatar.image = image;
}
OnePiece.PNG
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末季惩,一起剝皮案震驚了整個濱河市录粱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌画拾,老刑警劉巖啥繁,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異青抛,居然都是意外死亡旗闽,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門蜜另,熙熙樓的掌柜王于貴愁眉苦臉地迎上來适室,“玉大人,你說我怎么就攤上這事举瑰〉妨荆” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵此迅,是天一觀的道長汽畴。 經(jīng)常有香客問我,道長耸序,這世上最難降的妖魔是什么忍些? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮坎怪,結(jié)果婚禮上罢坝,老公的妹妹穿的比我還像新娘。我一直安慰自己搅窿,他們只是感情好嘁酿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布疾棵。 她就那樣靜靜地躺著,像睡著了一般痹仙。 火紅的嫁衣襯著肌膚如雪是尔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天开仰,我揣著相機(jī)與錄音拟枚,去河邊找鬼。 笑死众弓,一個胖子當(dāng)著我的面吹牛恩溅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谓娃,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼脚乡,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了滨达?” 一聲冷哼從身側(cè)響起奶稠,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎捡遍,沒想到半個月后锌订,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡画株,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年辆飘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谓传。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡蜈项,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出续挟,到底是詐尸還是另有隱情紧卒,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布庸推,位于F島的核電站常侦,受9級特大地震影響浇冰,放射性物質(zhì)發(fā)生泄漏贬媒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一肘习、第九天 我趴在偏房一處隱蔽的房頂上張望际乘。 院中可真熱鬧,春花似錦漂佩、人聲如沸脖含。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽养葵。三九已至征堪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間关拒,已是汗流浹背佃蚜。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留着绊,地道東北人谐算。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像归露,于是被迫代替她去往敵國和親洲脂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容