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

效果圖

如圖,通過點(diǎn)擊button進(jìn)入UIalertcontroller,選擇相冊進(jìn)入系統(tǒng)相冊選擇圖片并上傳到imageview上顯示。話不多說,上代碼:

#import "ViewController.m"

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
    
    UIImagePickerController *_imagePickerController;
}
@property (strong, nonatomic) IBOutlet UIImageView *imgv;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    //跳轉(zhuǎn)動畫效果
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    
}

- (IBAction)butt:(id)sender {
    
    UIAlertController *alertCtl =[[UIAlertController alloc]init];
    
    UIAlertAction *cancel =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"cancel");
    }];
    UIAlertAction *xiangji =[UIAlertAction actionWithTitle:@"相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"xiangji");
        
        [self openCamera];
    }];
    UIAlertAction *xiangce =[UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"xiangce");
        
        [self openPhotoLibrary];
    }];
    
    [alertCtl addAction:cancel];
    [alertCtl addAction:xiangji];
    [alertCtl addAction:xiangce];
    
    [self presentViewController:alertCtl animated:YES completion:nil];
}

/**
 *  調(diào)用照相機(jī)
 */
- (void)openCamera
{

    //判斷是否可以打開照相機(jī)
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        //攝像頭
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }
    else{
        NSLog(@"沒有攝像頭");
    }
}
/**
 *  打開相冊
 */
-(void)openPhotoLibrary{

    // 進(jìn)入相冊
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:_imagePickerController animated:YES completion:^{
            NSLog(@"打開相冊");
        }];
    }else{
        NSLog(@"不能打開相冊");
    }
}
#pragma mark - UIImagePickerControllerDelegate
// 拍照完成回調(diào)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
    
    NSLog(@"finish..");
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        //圖片存入相冊
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    self.imgv.image =image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

//進(jìn)入拍攝頁面點(diǎn)擊取消按鈕
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

接下來在info.plist文件中添加NSPhotoLibraryUsageDescription和NSCameraUsageDescription用于獲取手機(jī)的相機(jī)和相冊的權(quán)限,運(yùn)行。

需要注意的是,模擬器上并不支持相機(jī),選擇相冊,選擇圖片,完成。

完成.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • github排名https://github.com/trending,github搜索:https://gith...
    小米君的demo閱讀 4,958評論 2 38
  • 在本章中,您將要添加照片到 Homepwner 應(yīng)用程序。 您將呈現(xiàn)一個 UIImagePickerControl...
    titvax閱讀 676評論 0 1
  • 蘭花余韻浸幽香,小亭旁,語絲涼。 縱不相知,也把質(zhì)懷傷。 碧野蒼穹空悵惘,春水闊,界河長。 禪心懸處渡滄浪,夜茫茫...
    姀月閱讀 522評論 3 8
  • 很多人對無意義社交的放棄,別視為了“不合群”。 這篇文章告訴你問什么你不理解他們... 文字/ 果汁?? 有一種優(yōu)秀...
    一微之閣閱讀 645評論 1 4
  • 無法阻止時間流逝的步伐,記下那一幕幕四月的畫面。 越美麗的東西,越無法阻止他消逝的步伐; 曇花一現(xiàn)般稍縱即逝,因此...
    親不在頹廢閱讀 390評論 0 0

友情鏈接更多精彩內(nèi)容