MilkBottle
MilkBottle

Reputation: 4332

How to get image from UIImagePickerController and Pass to next VC

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 :
a) Bytes
b) Image

by means of using Segue or user class. If the Image file is huge , it is better pass by segue or class.

1) Get the Image or Bytes from the UIImagePickerController

2) How to pass the Image Bytes to next next VC
use Segue
or use class
Do I use user class to store the Image or Bytes?

3) How to get the Height and Width so that I can know if the image is in portrait or landscape mode.

4) How to check if there is image in ImageView?

I have a btn to click to check before navigating to next VC

Your help is greatly appreciated

Below is code I used:

   @IBOutlet var imageView: UIImageView!

let imagePicker = UIImagePickerController()

 @IBAction func loadImageButtonTapped(sender: UIButton) {
   imagePicker.allowsEditing = false
   imagePicker.sourceType = .PhotoLibrary

  presentViewController(imagePicker, animated: true, completion: nil)
}


//-- - UIImagePickerControllerDelegate Methods

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

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

  dismissViewControllerAnimated(true, completion: nil)
}

Upvotes: 1

Views: 19611

Answers (6)

Na el
Na el

Reputation: 67

Try this

dismiss(animated: true, completion: {
        self.performSegue(withIdentifier: "Whatever_segue", sender: nil)
    })

Upvotes: 0

Teo
Teo

Reputation: 3442

  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.

Upvotes: 7

AsifHabib
AsifHabib

Reputation: 950

For Swift 4.2

use following

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

}

Upvotes: 3

Valentin
Valentin

Reputation: 3302

Use the imagePickerController didFinishPickingImage instead:

1 & 2)

    // Class variable
    var image: UIImage?
    ...
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 

        self.image = image
        picker.dismissViewControllerAnimated(true, completion: { (finished) -> Void in
            // Perform your segue to your next VC
        })
    }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// One of the two segue.destinationViewController.isKindOfClass(YourDestinationViewController) {
    if segue.identifier == "Your Destination View Controller Identifier" {
            let destinationViewController = segue.destinationViewController as! YourDestinationViewController
            destinationViewController.image = self.image
        }
    }

3)

image.size.height
image.size.width

Upvotes: 1

Meghs Dhameliya
Meghs Dhameliya

Reputation: 2444

1) Get image from UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    //image object       
}

2) To pass image to other ViewController Make property of UIimage in Second VC and assign image object to it.

secondVC.imageProperty = image;

3)Get Hight and width of Image

NSLog(@'image height: %f',image.size.height);
NSLog(@'image width: %f',image.size.width);

Upvotes: 1

Kirit Modi
Kirit Modi

Reputation: 23407

    picker.dismissViewControllerAnimated(true, completion: { () -> Void in
                let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

     // Convert image to data and Send data anotherViewController 

       image data 

      UIImageJPEGRepresentation(self.pickedImage, 0.5)
            })

Upvotes: 1

Related Questions