ios-获取相册相机图片


title: ios-获取相册相机图片
categories: Ios
tags: [ios, 相册, 相机]
date: 2021-02-11 16:29:28
comments: false
mathjax: true
toc: true

ios-获取相册相机图片


前篇

  • 隐私信息 info.plist 配置: https://stackoverflow.com/questions/39465687/nscamerausagedescription-in-ios-10-0-runtime-crash
  • iOS关于相机相册权限设置 - https://www.jianshu.com/p/84df2ca84ade
  • iOS 获取图片(拍照、相册、图库)详解 - https://www.jianshu.com/p/39a6a104d3c1
  • iOS 拍照(相机)/相册获取图片 - https://www.jianshu.com/p/5465e87d8a0a
  • iOS关于相机相册权限设置 - https://www.jianshu.com/p/84df2ca84ade
  • iOS开发:调用系统自带相机以及获取相册照片的功能实现 - https://www.cnblogs.com/sundaysgarden/articles/9188975.html

获取相册图片

  1. info.plist 中配置相册权限提示:

    Key    :  Privacy - Photo Library Usage Description    
    Value  :  $(PRODUCT_NAME) photo use
    
  2. 实现 <UIKit/UIKit.h>UIImagePickerControllerDelegate 代理接口

    // 当用户选取完成后调用;
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { ... }
    // 当用户取消选取时调用;
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { ... }
    
  3. 获取相册

    #import <Photos/Photos.h>
    
    // 检查相册是否可用
    +(BOOL)checkGallery{
        PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
        if (authStatus == PHAuthorizationStatusRestricted || authStatus == PHAuthorizationStatusDenied) {
            return NO;
        }
        return YES;
    }
    
    // 获取相册
    -(void)getGallery:(CPicture*)data cb:(CodeMsgFn)cb {
        self.data = data;
        self.cb = cb;
        
        //本地相册不需要检查,因为UIImagePickerController会自动检查并提醒
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = data.isEdit; // 设置选择后的图片可被编辑
        
        BOOL isOk = [PicHelper checkGallery]; // 先检查相机可用是否
        if (isOk == NO) {
            [self doCallback:ECodePermissionDenyError msg:@"--- no gallery permission"];
        } else {
            [Tool presentVC:picker]; // 打开相册
        }
    }
    

相机拍照

  1. info.plist 中配置相机权限提示:

    Key    :  Privacy - Camera Usage Description   
    Value  :  $(PRODUCT_NAME) camera use
    
  2. 实现 <UIKit/UIKit.h>UIImagePickerControllerDelegate 代理接口 (和相册一样)

    // 当用户选取完成后调用;
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { ... }
    // 当用户取消选取时调用;
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { ... }
    
  3. 打开相机

    #import <AVFoundation/AVCaptureDevice.h>
    #import <AVFoundation/AVMediaFormat.h>
    
    // 检查相机是否可用
    +(BOOL)checkCamera{  // 如果用户点击 不允许, 则为 0
        NSString *mediaType = AVMediaTypeVideo;
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        if(AVAuthorizationStatusRestricted == authStatus || AVAuthorizationStatusDenied == authStatus) {
            return NO;
        }
        return YES;
    }
    
    // 相机拍照
    -(void)takePicture:(CPicture*)data cb:(CodeMsgFn)cb {
        self.data = data;
        self.cb = cb;
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = data.isEdit; // 设置拍照后的图片可被编辑
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            BOOL isOk = [PicHelper checkCamera]; // 先检查相机可用是否
            if (isOk == NO) {
                [self doCallback:ECodePermissionDenyError msg:@"--- no camera permission"];
            } else {
                [Tool presentVC:picker]; // 打开相机
            }
        } else {
            [self doCallback:ECodeUnknown msg:@"--- not isSourceTypeAvailable, you device may be iphonesimulator"];
        }
    }
    
  4. 测试


踩坑

调用拍照闪退

报错信息: he app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data

就是需要在 info.plist 中配置相机提示.

Guess you like

Origin blog.csdn.net/yangxuan0261/article/details/113797512