由于某种原因,在我的新项目中,此代码无法正常运行,而之前该代码对我来说一直有效。当前代码不会更改profileImage
的用户界面。
代表
UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate
代码:
@IBOutlet weak var profileImage: UIImageView!
@IBAction func changeProfilePicture(_ sender: Any) {
print("Profile picture tapped")
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
let alertController = UIAlertController(title: "Add Picture", message: "", preferredStyle: .actionSheet)
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alertController.addAction(photoLibraryAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : Any]?) {
self.profileImage.image = image
self.dismiss(animated: true, completion: nil)
}
控制台输出
发现扩展名时遇到错误:错误 域= PlugInKit代码= 13“查询已取消” UserInfo = {NSLocalizedDescription =查询已取消}
我尝试过
@objc func internal func @objc internal func
self.profileImage.image = image
未设置用户界面并更改图像
答案 0 :(得分:2)
正确的委托方法
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
self.profileImage.image = image
}
else
if let image = info[.editedImage] as? UIImage {
self.profileImage.image = image
}
self.dismiss(animated: true, completion: nil)
}