为什么 UIImagePickerController 不委托给我的 Object 类? [英] Why doesn't UIImagePickerController delegate to my Object class?

查看:24
本文介绍了为什么 UIImagePickerController 不委托给我的 Object 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class CameraPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var viewController:MyProfileVC!

 func launchCamera() {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let imagePicker:UIImagePickerController = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
            imagePicker.cameraCaptureMode = .photo
            imagePicker.allowsEditing = false

            self.viewController.present(imagePicker, animated: true, completion: nil)
        } }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
        print("didFinishPickingMedia")
}

这是我的对象类函数,但在拍照后没有调用didFinishPickingMediaWithInfo"函数.此外,呈现图像选择器的视图控制器是一个不同的 Swift 文件

This is my object class function, but 'didFinishPickingMediaWithInfo' function doesn't get called after taking the picture. Also, the viewcontroller which is presenting the imagepicker is a different Swift file

推荐答案

我遇到了同样的问题,我已经找到了解决方案,所以我发布了我的版本(我正在从照片库中拍照,但它是一样的 :) ).

I had the same problem and I've found a solution, so I'm posting my version (I'm taking a picture from the photo library but it's the same :) ).

我遇到了内存管理问题.我已经创建了一个 IBAction 函数,我在其中实例化了我的相机处理程序类(里面有委托......).在函数结束时,变量超出范围并被释放.为了解决这个问题,我把它作为实例变量.

I had a memory management issue. I've created an IBAction function where I instantiated my camera handler class (with the delegate inside...). At the end of the function the variable goes out of scope and it's deallocated. To solve the issue I've made it as instance variable.

这是我使用 UiButton 为 VC 编写的代码:

That's my code for the VC with my UiButton:

class STECreateUserVC: UIViewController {

    @IBOutlet weak var imgAvatar: UIImageView!
    let cameraHandler = STECameraHandler()

    @IBAction func buttonPressed(_ sender: UIButton) {
        cameraHandler.importPictureIn(self) { [weak self] (image) in
            self?.imgAvatar.image = image
        }
    }
}

...这就是我的处理程序:

...and that's my handler:

class STECameraHandler: NSObject {

    let imagePickerController = UIImagePickerController()
    var completitionClosure: ((UIImage) -> Void)?

    func importPictureIn(_ viewController: UIViewController, completitionHandler:((UIImage) -> Void)?) {
        completitionClosure = completitionHandler
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        imagePickerController.sourceType = .photoLibrary
        imagePickerController.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        viewController.present(imagePickerController, animated: true, completion: nil)
    }
}

extension STECameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let completitionClosure = completitionClosure, let image = info[UIImagePickerControllerEditedImage] as? UIImage {
            completitionClosure(image)
        }
        imagePickerController.dismiss(animated: true)
    }
}

为了让代码更简洁,我使用了闭包.

I've used a closure in order to have a cleaner code.

这篇关于为什么 UIImagePickerController 不委托给我的 Object 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆