Why doesn't UIImagePickerController delegate to my Object class?

元气小坏坏 提交于 2019-12-25 07:48:40

问题


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

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


回答1:


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 :) ).

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.

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.




回答2:


This:

self.viewController.present(imagePicker, animated: true, completion: nil)

should be:

self.present(imagePicker, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/41195878/why-doesnt-uiimagepickercontroller-delegate-to-my-object-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!