gpt4 book ai didi

ios - ImagePicker 问题并分配给 ImageView

转载 作者:行者123 更新时间:2023-11-28 13:47:44 24 4
gpt4 key购买 nike

我正在构建一个小应用程序,允许用户选择要附加到帖子的图像。用户选择添加图像(具有点击手势识别器)并显示 iOS 照片库。然后,用户选择图像,所选图像应替换默认的相机图像。从库中选择图像时出现以下错误:2019-03-16 19:13:09.238959-0400 Spirit-App[34772:2211555] [discovery] 发现扩展时遇到的错误:Error Domain= PlugInKit Code=13“查询已取消”UserInfo={NSLocalizedDescription=查询已取消}。我有 info.plist 设置 enter image description here

根据我用于调试的打印语句,我似乎进入了 imageTapped 函数,但从未进入 @objc func imagePickerController。即使我注释掉 @objc func imagePickerController 我仍然收到上面的错误。那么,两个问题:

  1. 为什么即使我没有在模式页面上重新分配所选图像,我也会收到错误消息?
  2. 如果我解决了这个问题,我拥有的用于分配图像的代码是否可以正常工作或是否需要更改?

代码:

class NewPostViewController: ViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var newPostImage: UIImageView!
@IBOutlet weak var postDescription: UITextView!

let imagePicker = UIImagePickerController()

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
imagePicker.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tap:)))
newPostImage.isUserInteractionEnabled = true
newPostImage.addGestureRecognizer(tap)
}

@objc func imageTapped(tap: UITapGestureRecognizer) {
print("BUTTON WORKED!")
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary

present(imagePicker, animated: true, completion: nil)
}

@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})

print("Image Picker Complete")
newPostImage.image = image
dismiss(animated: true, completion: nil)
}
}

最佳答案

您的委托(delegate)方法签名错误。

改变:

@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){

到:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

您还需要更改该委托(delegate)中的所有代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
newPostImage.image = image
}

picker.dismiss(animated: true, completion: nil)
}

关于ios - ImagePicker 问题并分配给 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55202510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com