gpt4 book ai didi

ios - 如何将 UIImagePickerController 与委托(delegate)一起使用?

转载 作者:行者123 更新时间:2023-11-30 11:43:28 32 4
gpt4 key购买 nike

我将 UIView(A 类)作为 xib 实例添加到 ViewController。

我想通过 Action (在A类中)从ViewController类的UIImagePickerController获取图像并将其返回给A类。

我编写了以下代码。但是,我不知道如何回到A类。

我再次向 ViewController 委托(delegate),但我放弃了它,因为我显示了委托(delegate)的各种错误。 [展开错误...,只是没有移动任何东西...等等]。

// A Class
class A: CustomDelegate{
var delegate001: PhotoLibraryDelegate!

class func instance() -> A {
return UINib(nibName: "A", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! A
}

@IBAction func openPhotoLibrary(_ sender: Any) {
self.delegate001.selectPhoto() //I want photo in A Class XD. so make Delegate!!
}
}

@objc protocol PhotoLibraryDelegate {
func selectPhoto()
}

//ViewController Class
class ViewController: PhotoLibraryDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let a = A.instance()

override func viewDidLoad() {
super.viewDidLoad()
a.delegate006 = self
}

func selectPhoto() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let pickerView = UIImagePickerController()
pickerView.sourceType = .photoLibrary
pickerView.delegate = self

self.present(pickerView, animated: true)
}
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!

self.dismiss(animated: true)

//... Wa!? I want to return "image" to A class. OMG.
}
}

最佳答案

最简单的方法是您已经创建了 A 类的实例。现在直接将图像分配给它即可。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!

a.imageView.image = image

self.dismiss(animated: true)


}

关于ios - 如何将 UIImagePickerController 与委托(delegate)一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49084832/

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