UIImagePickerController 实况照片

Posted

技术标签:

【中文标题】UIImagePickerController 实况照片【英文标题】:UIImagePickerController Live Photo 【发布时间】:2016-09-14 11:36:27 【问题描述】:

我正在尝试显示一个 UIImagePickerController,它允许用户选择 UIImage 或 PHLivePhoto。

import Photos

var imagePicker = UIImagePickerController()

if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) 

     imagePicker.delegate = self
     imagePicker.sourceType = .savedPhotosAlbum

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

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

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“源 2 没有可用类型”

【问题讨论】:

您希望选定的实况照片返回mediaTypekUTTypeImagekUTTypeLivePhoto 吗? 【参考方案1】:

所以 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];
;

希望这会有所帮助。

【讨论】:

如果你不导入,那么你会得到code.swift:141:49: Use of unresolved identifier 'kUTTypeLivePhoto' 如果我使用你提供的代码,那么我会得到问题中的错误。 (我在 Xcode 10.1 上)【参考方案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]) 
   

【讨论】:

这让媒体浏览器可以看到照片库中的实况照片,但它会按照问题的要求进行吗?我发表了评论以获得澄清。

以上是关于UIImagePickerController 实况照片的主要内容,如果未能解决你的问题,请参考以下文章

UIImagePickerController 不是全屏

UIImagePickerController、自定义 UIButton 和 AutoLayout

UIImagePickerController 实况照片

OCMock 模拟 UIImagePickerController

UIImagePickerController 图像没有改变

无法弹出 UIImagePickerController