UIImagePickerController 实时照片

UIImagePickerController Live Photo

我正在尝试显示允许用户select UIImage 或 PHLivePhoto 的 UIImagePickerController。

import Photos

var imagePicker = UIImagePickerController()

if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {

     imagePicker.delegate = self
     imagePicker.sourceType = .savedPhotosAlbum

     imagePicker.mediaTypes = ["kUTTypeImage","kUTTypeLivePhoto"]
}

显示视图控制器时,出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'No available types for source 2'

 var imag:UIImagePickerController?

     print("Camera")
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            print("Button capture")
            self.imag = UIImagePickerController()
            self.imag!.delegate = self
            self.imag!.sourceType = UIImagePickerControllerSourceType.Camera
            self.imag!.cameraDevice = UIImagePickerControllerCameraDevice.Front
            self.imag!.mediaTypes = [kUTTypeImage as String]
            self.imag!.allowsEditing = false
            self.presentViewController(self.imag!, animated: true, completion: nil)
        }

        print("Gallery")
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
            print("Button capture")
            self.imag = UIImagePickerController()
            self.imag!.delegate = self
            self.imag!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            //imag.mediaTypes = [kUTTypeImage];
            self.imag!.allowsEditing = false
            self.presentViewController(self.imag!, animated: true, completion: nil)
        }

  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
   }

所以 kUTTypeLivePhoto 只能从 ios 9.1 及更高版本使用,因此您可能应该考虑进行版本检查。另外,如果你

import MobileCoreServices

可以直接使用kUTTypeLivePhoto和kUTTypeImage

if #available(iOS 9.1, *) {
    picker.mediaTypes = [kUTTypeLivePhoto as String, kUTTypeImage as String
} else {
    picker.mediaTypes = [kUTTypeImage as String];
};

希望对您有所帮助。