Search code examples
iosswiftfunctioncamerauiimagepickercontroller

Camera photo selection not working in Swift


Why isn't the second function being called when the user takes the photo and clicks the use photo button? The view gets dismissed, but the print statement doesn't work and the function isn't called.

@IBAction func openCamera(_ sender: UIBarButtonItem)
    {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        print("made it")
        createImage(image: image)
        self.dismiss(animated: true, completion: nil);
    }

Solution

  • Using Swift 3, here's what I have. My code is meant to have (1) a select view controller, which uses the UIImagePickerController to either use the camera or select from the camera roll, then (2) sequel to an edit view controller I stripped out the code for the buttons, as I'm not using IB.

    class SelectViewController: UIViewController {
    
        // selection and pass to editor
    
        let picker = ImagePickerController()
        var image = UIImage()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            picker.delegate = self
        }
    
    extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        // MARK: Camera App
    
        func openCameraApp() {
            if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
                picker.allowsEditing = false
                picker.sourceType = UIImagePickerControllerSourceType.camera
                picker.cameraCaptureMode = .photo
                picker.modalPresentationStyle = .fullScreen
                present(picker,
                        animated: true,
                        completion: nil)
            } else {
                noCamera()
            }
        }
        func noCamera(){
            let alertVC = UIAlertController(
                title: "No Camera",
                message: "Sorry, this device has no camera",
                preferredStyle: .alert)
            let okAction = UIAlertAction(
                title: "OK",
                style:.default,
                handler: nil)
            alertVC.addAction(okAction)
            present(
                alertVC,
                animated: true,
                completion: nil)
        }
    
        // MARK: Photos Albums
    
        func showImagePicker() {
            picker.allowsEditing = false
            picker.sourceType = .photoLibrary
            present(picker,
                    animated: true,
                    completion: nil)
            picker.popoverPresentationController?.sourceView = self.view
        }
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            image = chosenImage
            self.performSegue(withIdentifier: "ShowEditView", sender: self)
            dismiss(animated: true, completion: nil)
        }
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: false, completion: nil)
        }
    
        // MARK: Seque to EditViewController
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "ShowEditView" {
                if let vc = segue.destination as? EditViewController {
                    vc.image = image
                }
            }
        }
    
    }
    

    If you aren't segueing to another VC, remove the .performSegue call and the code below the the final MARK: notation. (The camera/selected image is in the image var.)