UIImageView not updating with new image after picture taken or picture chosen from photo library

不问归期 提交于 2020-01-03 05:19:45

问题


I'm trying to populate an image within a UIImageView after I either take a picture or choose a picture from my photo library.

here is the code handling the camera and photo library activations:

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


@IBAction func activatePhotoLib(_ sender: Any) {
    if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
}

Here is the code that populates the image within the UIImageView:

@IBOutlet weak var imageView: UIImageView!

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.imageView.contentMode = .scaleAspectFit
    self.imageView.image = image
    self.dismiss(animated: true, completion: nil);
}

currently after I take a picture with my camera or choose an image from my photo library, I'm redirected to the initial view of the app without UIImageView space updated with the photo.

Any help is appreciated thanks!


回答1:


In Swift 3 you need permission to access photoLibrary by adding below keys to your plist and you need to use the proper delegate method.

Add UIImagePickerControllerDelegate& UINavigationControllerDelegate to your classand set imagePicker delegate to viewDidLoad & Try below code.

    let imagePicker = UIImagePickerController()

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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
     }
    picker.dismiss(animated: true, completion: nil);
    }

Output: updated




回答2:


This seems to be a problem related to XCode failing to explain that the imagePickerController function syntax has changed (since Swift 3). You will find these two ways of writing the function all over the web. You will also be able to use both in XCode without getting any errors. But only the second one will actually get the image properly.

(Option 1 - doesn't work, but XCode doesn't flag it):
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { }

(Option 2 - this is the right syntax!):
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { }



回答3:


**You need to save the UIImagePickerController like Global Object, after that, you need push this code on your action function **

@objc func getImage() {

    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

    imagePicker.allowsEditing = false

    imagePicker.modalPresentationStyle = .overCurrentContext

    present(imagePicker, animated: true)

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    imageView.image = image
 }

self.dismiss(animated: true, completion: nil);

}


来源:https://stackoverflow.com/questions/40826760/uiimageview-not-updating-with-new-image-after-picture-taken-or-picture-chosen-fr

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