[英]Swift 4 Image Picker Not Changing UIImageView
出於某種原因,在我的新項目中,此代碼無法正常工作,之前對我來說一直有效。 當前代碼不會更改profileImage
的ui。
代表們
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)
}
控制台輸出
發現擴展名時遇到的錯誤:錯誤Domain = PlugInKit代碼= 13“查詢已取消” UserInfo = {NSLocalizedDescription =查詢已取消}
我努力了
@objc func internal func @objc internal func
self.profileImage.image = image
不會設置用戶界面並更改圖像
正確的委托方法
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.