Why doesn't UIImagePickerController delegate t

2019-09-05 04:24发布

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

2条回答
小情绪 Triste *
2楼-- · 2019-09-05 05:04

This:

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

should be:

self.present(imagePicker, animated: true, completion: nil)
查看更多
欢心
3楼-- · 2019-09-05 05:18

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.

查看更多
登录 后发表回答