简体   繁体   中英

Saving photo to image in Swift 4.2

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和按钮

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()


}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM