今天,我意识到当我将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)
}
答案 0 :(得分:3)
长手势具有状态
@IBAction func ppLongPressed(_ sender: UILongPressGestureRecognizer) {
if sender.state != .began { return }
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}