Saving photo to image in Swift 4.2

250 views Asked by At

I am having a problem displaying the photo I just took or chose to the image view.

Below is a pic of the imagview on the left and the licencePhoto button on right.

imageview and button

When I click on Take Photo, it allows me to take a pic or select from my library, as the code shows:

@IBAction func seclectOrTakePhoto(_ sender: Any) {

    pickerController.delegate = self
    pickerController.allowsEditing = true

    let alertController = UIAlertController(title: "Add a Picture", message: "Choose From", preferredStyle: .actionSheet)

    let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
        self.pickerController.sourceType = .camera
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
        self.pickerController.sourceType = .photoLibrary
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
        self.pickerController.sourceType = .savedPhotosAlbum
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

    alertController.addAction(cameraAction)
    alertController.addAction(photosLibraryAction)
    alertController.addAction(savedPhotosAction)
    alertController.addAction(cancelAction)


    present(alertController, animated: true, completion: nil)

}

But, it does not display the photo to the licensePhoto image and in Firebase, it is uploading the pic I have displayed in the image view as below and not the one I just took with phone.

How can I achieve this?

Edited

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.licensePhoto.image = image
    self.dismiss(animated: true, completion: nil)


    // any time the photo changes, check the button status
    updateSignupButtonStatus()


}
1

There are 1 answers

0
LizG On BEST ANSWER

After searching and looking through tutorials, I finally got something to work:

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    licensePhoto.contentMode = .scaleAspectFit
    licensePhoto.image = chosenImage
    pickerController.dismiss(animated: true, completion: nil)
}