xcode 发现扩展时遇到 Swift 4 错误 - 照片不会在 UI 上加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49733003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:36:54  来源:igfitidea点击:

Swift 4 errors encountered while discovering extensions - photo will not load on UI

iosswiftxcode

提问by sixstringsteph

I am getting the following error message when I choose a photo from the photo library to populate a UIImageView in XCode 9.2:

当我从照片库中选择一张照片以填充 XCode 9.2 中的 UIImageView 时,我收到以下错误消息:

  • [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
  • [发现] 发现扩展时遇到的错误:Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

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.

模拟器能够访问照片库,我可以查看照片以进行选择,但是当我单击“选择”选项来选择照片时,会抛出错误,并且在选择器被解除后,图像是不填充 UIImageView。

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!

我已经搜索了 Stack Overflow,如果我执行此步骤,我能够摆脱错误消息:从 Xcode 菜单打开:产品 > 方案 > 编辑方案 > 在您的环境变量中设置 OS_ACTIVITY_MODE 在值集禁用。但是,这只能消除错误,并不能解决我选择的照片未填充 UIImageView 的问题。我是 Swift 和 Xcode 的新手并且被卡住了!请帮忙!

Here is my code:

这是我的代码:

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)
        }

    }
}

回答by vadian

Two issues:

两个问题:

  • The signature of didFinishPickingMediaWithInfois 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)
    }
    
  • 的签名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)
    }