当我从照片库中选择一张照片来填充XCode 9.2中的UIImageView时,我收到以下错误消息:
- [发现] 发现扩展时遇到的错误: 错误域=插件套件代码=13 "查询已取消" 用户信息={NSLocalizedDescription=查询已取消}
模拟器能够访问照片库,我能够查看照片以进行选择,但是当我单击"选择"选项以选择照片时,会抛出错误,并且在选择器被关闭后,图像不会填充UIImageView。
我已经搜索了堆栈溢出,如果我执行此步骤,则可以摆脱错误消息: 从 Xcode 菜单打开:产品>方案> 编辑方案> 在您的环境中 变量设置OS_ACTIVITY_MODE在值集中禁用。 但是,这只能消除错误,并不能解决我所选照片未填充UIImageView的问题。 我是 Swift 和 Xcode 的新手,被困住了! 请帮忙!
这是我的代码:
import UIKit
class HomeVC: UIViewController, UINavigationControllerDelegate,
UIImagePickerControllerDelegate {
@IBOutlet weak var avaImg: UIImageView!
@IBOutlet weak var usernameLbl: UILabel!
@IBOutlet weak var fullnameLbl: UILabel!
@IBOutlet weak var emailLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//get user details from user global var (from database)
let username = (user!["username"] as? String)?.uppercased()
let fullname = user!["fullname"] as? String
let email = user!["email"] as? String
let ava = user!["ava"] as? String
//populate labels on view
usernameLbl.text = username
fullnameLbl.text = fullname
emailLbl.text = email
}
@IBAction func edit_click(_ sender: AnyObject) {
//select ava
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
picker.allowsEditing = true
self.present(picker, animated: true, completion: nil)
//selected image
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerEditedImage] as?
UIImage
avaImg.image = image
self.dismiss(animated: true, completion: nil)
}
}
}
两个问题:
-
didFinishPickingMediaWithInfo
的签名是错误的。在 Swift 3+ 中它是func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
-
委托方法必须位于类的顶层(而不是在其他方法中)
@IBAction func edit_click(_ sender: Any) { //select ava let picker = UIImagePickerController() picker.delegate = self picker.sourceType = .photoLibrary picker.allowsEditing = true self.present(picker, animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //selected image let image = info[UIImagePickerControllerEditedImage] as? UIImage avaImg.image = image self.dismiss(animated: true, completion: nil) }