How to get image from UIImagePickerController and Pass to next VC

后端 未结 6 1957
心在旅途
心在旅途 2021-01-15 22:41

I select a photo from PhotoLibrary and How can I achieve below tasks

In this case, I am using Swift. I need to reconstruct the image in the next VC either thru :

6条回答
  •  有刺的猬
    2021-01-15 23:04

    1. You need to assign the delegate to your image picker so that the appropriate methods to be called. In your loadImageButtonTapped function, don't forget to assign the image picker delegate.

    Then, you should implement this method:

        func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){ 
            // check if an image was selected, 
            // since a images are not the only media type that can be selected
            if let img = info[.originalImage] {
                methodToPassImageToViewController(img)
        }
    

    If you need to find out the size of the image, you cann access it's size property.

    1. To pass the image to another view controller, you could use one of these two options:

      • you could use delegation
      • you could use this function for when you navigate from your view controller to another one using storyboard segues:

        func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) 
        {
             var destinationController = segue.destinationViewController
             // now you can pass the image to the destination view controller
        }
        

    P.S: didFinishPickingImage is deprecated since iOS 3, you should not use that method.

提交回复
热议问题