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

标签 ios swift uiimagepickercontroller

我将 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/

相关文章:

iphone - 以编程方式滚动到 UICollectionView 中的补充 View

ios - 如何在 OpenGL iOS 中制作气泡?

php - 由另一种语言的类创建的 Swift 数组

ios - 关闭 UIImagePickerController 后屏幕尺寸错误

ios - _UIImagePickerControllerUserDidCaptureItem 是 UIImagePickerController 的私有(private) API 吗?

ios - TableView 单元格中的 ScrollView 将无法正确显示图像,并且 swift 4 中的分页模式下有一页额外的页面

caching - ASNetworkImageNode缓存

ios - 如何从另一个 View Controller 加载webview?

swift - BluetoothLE 会扰乱定时器计时吗?

iphone - 在 iPad 中显示来自 UIActionSheet 的 uiimagepickercontrollersourcetypephotolibrary 的 UIImagePickerViewController