Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...