影片imagePickerController取消无法运作

时间:2019-09-06 12:43:47

标签: swift video uiimagepickercontroller

我正在为照片和视频使用自定义的CropViewController开源imagePicker,由于CropViewController没有视频选项,我正尝试使用Swift本身提供的默认imagePicker。

从照片库中选择视频后,底部显示三个按钮(取消,播放和选择)。播放按钮和选择按钮效果很好,但取消则无效。

这是我的代码,用于为照片和视频触发imagePickerController。

@objc func videoPresentPicker() {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.mediaTypes = [kUTTypeMovie as String]
        picker.allowsEditing = true
        self.present(picker, animated: true, completion: nil)
    }

@objc func photoPresentPicker() {
        self.croppingStyle = .default
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = false
        self.present(picker, animated: true, completion: nil)
}

非常感谢您的帮助。我已经奋斗了几天,终于伸出援手……

更新

extension ChatViewController: CropViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {
            let data = NSData(contentsOf: videoUrl as URL)!
            print("File size before compression: \(Double(data.length / 1048576)) mb")
            let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v")
            self.compressVideo(inputURL: videoUrl as URL, outputURL: compressedURL) { (exportSession) in
                guard let session = exportSession else {
                    return
                }
                switch session.status {
                case .unknown:
                    break
                case .waiting:
                    break
                case .exporting:
                    break
                case .completed:
                    guard let compressedData = NSData(contentsOf: compressedURL) else {
                        return
                    }
                    print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
                case .failed:
                    break
                case .cancelled:
                    break
                @unknown default:
                    break
                }
            }
        } else {
            guard let image = (info[UIImagePickerController.InfoKey.originalImage] as? UIImage) else { return }
            let cropController = CropViewController(croppingStyle: croppingStyle, image: image)
            cropController.delegate = self

            imageView.image = image

            picker.dismiss(animated: true, completion: {
                self.present(cropController, animated: true, completion: nil)
                if self.inputTextField.isFirstResponder == true {
                    self.handleKeyboardWillShow()
                }

            })
        }
        transparentView.alpha = 0
        self.tableView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        dismiss(animated: true, completion: nil)
    }

1 个答案:

答案 0 :(得分:0)

只需实现此功能

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated:true, completion: nil)
}