Swift 5-选择大视频时,ImagePickerController使应用程序崩溃

时间:2019-06-01 05:14:00

标签: swift uiimagepickercontroller

我想从图片库中提取视频,并使用以下代码。问题是,每次我拾取较大的视频(假定大小超过15 MB)时,在压缩视频时我的应用程序就会崩溃。基本上,它永远不会完成压缩部分,一旦我单击choose选项,应用程序就会崩溃。

仅当我选择.videoQuality = .typeMediumhigh时才会发生这种情况。由于压缩从未发生,因此realivePath变为nil。

有人可以告诉我我在做什么错吗?另外,我不是在实际设备上测试它,而是在使用模拟器。

@objc func imageViewTapped(_ recognizer: UITapGestureRecognizer) {
    imagePicker.sourceType = .photoLibrary
    imagePicker.mediaTypes = ["public.movie"]
    imagePicker.videoQuality = .typeMedium
    present(imagePicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    let videoURL = info[.mediaURL] as? NSURL
//        self.videoURLRelativePath = videoURL!.filePathURL
    guard let relativePath = videoURL!.filePathURL else {return}
    print("Video File Path is \(relativePath)")
    self.videoURLRelativePath = relativePath

    do {
        //Generate Video Asset thumbnail
        asset =   AVAsset(url: videoURL! as URL)
        let imageGenerator =   try AVAssetImageGenerator(asset: asset)
        let cgImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
        let thumbnailImage = UIImage(cgImage: cgImage)
        uploadPostImageView.image = thumbnailImage
        self.selectedImage = thumbnailImage

    } catch let error as NSError {
        print(error)
    }

    dismiss(animated: true, completion: nil)
}

.photoLibrary提取较大的视频(超过15 MB)时,我还有其他需要考虑的问题吗?或推荐的任何快速库?

这是错误消息:

  

致命错误:解开可选值时意外发现nil

这来自:

guard let relativePath = videoURL!.filePathURL else {return}

请注意,filePathURL并非仅用于较大的视频。

0 个答案:

没有答案