调用相册和相机

1.调用相册

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
        if(UIImagePickerController.isSourceTypeAvailable(.photoLibrary))
        {
            if(self.PhotoLibraryPermissions() == true){
                let sourceType = UIImagePickerControllerSourceType.photoLibrary
                let imagePickerController:UIImagePickerController = UIImagePickerController()
                imagePickerController.delegate = self   
                imagePickerController.sourceType = sourceType
                controller?.present(imagePickerController, animated: true, completion: nil)
            }
        }

调用相册的权限判定

1
2
3
4
5
6
7
8
    func PhotoLibraryPermissions() -> Bool {
        let library:PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        if(library == PHAuthorizationStatus.denied || library == PHAuthorizationStatus.restricted){
            return false
        }else {
            return true
        }
    }

2.调用相机

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 if(UIImagePickerController.isSourceTypeAvailable(.camera))
        {
            if(self.cameraPermissions() == true){
                let sourceType = UIImagePickerControllerSourceType.camera
                let imagePickerController:UIImagePickerController = UIImagePickerController()
                imagePickerController.delegate = self   
                imagePickerController.sourceType = sourceType
                controller?.present(imagePickerController, animated: true, completion: nil)
            }
        }

调用相机的权限判定

1
2
3
4
5
6
7
8
 func cameraPermissions() -> Bool {
        let authStatus:AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        if(authStatus == AVAuthorizationStatus.denied || authStatus == AVAuthorizationStatus.restricted) {
            return false
        }else {
            return true
        }
    }

3.调用后返回接口的实现

1
2
3
4
5
6
7
8
9
extension StampImageSelect : UIImagePickerControllerDelegate,UINavigationControllerDelegate{  //点击OK时实现
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {        
        var image = info[UIImagePickerControllerOriginalImage] as! UIImage    //相册中选择或相机中拍摄的图片   
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {  //点击Cancel时实现
        picker.dismiss(animated: true, completion: nil)
    }
}