发现扩展时遇到Swift 4错误 - 在UI上不会加载照片 [英] Swift 4 errors encountered while discovering extensions - photo will not load on UI
问题描述
当我从照片库中选择照片以在XCode 9.2中填充UIImageView时,我收到以下错误消息:
I am getting the following error message when I choose a photo from the photo library to populate a UIImageView in XCode 9.2:
- [发现扩展时遇到的错误:错误Domain = PlugInKit Code = 13查询已取消UserInfo = {NSLocalizedDescription =查询已取消}
模拟器可以访问照片库,我可以查看照片进行选择,但是当我点击选择选项选择照片时,会抛出错误,并且在选择器被取消后图像是没有填充UIImageView。
The simulator is able to access the photo library and I am able to view the photos to make a selection, but when I click on the 'Choose' option to select a photo, the error is thrown and after the picker is dismissed the image is not populating the UIImageView.
我搜索了Stack Overflow,如果我执行此步骤,我可以摆脱错误消息:从Xcode菜单打开:产品> Scheme> Edit Scheme> On your环境变量在值集disable中设置OS_ACTIVITY_MODE。但是,这只是摆脱了错误,并没有修复我选择的照片没有填充UIImageView的问题。我是Swift和Xcode的新手而且卡住了!请帮忙!
I have searched Stack Overflow and am able to get rid of the error message if I do this step: From Xcode menu open: Product > Scheme > Edit Scheme > On your Environment Variables set OS_ACTIVITY_MODE in the value set disable. However, this only gets rid of the error and does not fix the issue with my selected photo not populating the UIImageView. I am new to Swift and Xcode and am stuck! Please help!
这是我的代码:
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+中它是
The signature of
didFinishPickingMediaWithInfo
is wrong. In Swift 3+ it is
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any])
委托方法必须位于类的顶层(不是在另一个方法中) )
The delegate method must be on the top level of the class (not in another method)
@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)
}
这篇关于发现扩展时遇到Swift 4错误 - 在UI上不会加载照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!