UIImagePickerController与LongPressGesture一起崩溃

时间:2018-08-17 19:42:54

标签: swift xcode uiimagepickercontroller

今天,我意识到当我将UIImagePickerController与一个按钮一起使用时,它可以正常工作。但是,当我想通过LongPressGesture呈现它时,该应用程序在运行时崩溃。为什么这样?

操作:

@IBAction func ppLongPressed(_ sender: UILongPressGestureRecognizer) {
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    present(imagePicker, animated: true, completion: nil)
}

委托:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        self.profileImage.image = image

    }
    dismiss(animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:3)

长手势具有状态

@IBAction func ppLongPressed(_ sender: UILongPressGestureRecognizer) {
    if sender.state != .began { return }
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    present(imagePicker, animated: true, completion: nil)
}