How to get image from UIImagePickerController and Pass to next VC

后端 未结 6 1953
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2021-01-15 23:04

    For Swift 4.2

    use following

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    
    }
    
    0 讨论(0)
  • 2021-01-15 23:12

    Try this

    dismiss(animated: true, completion: {
            self.performSegue(withIdentifier: "Whatever_segue", sender: nil)
        })
    
    0 讨论(0)
  • 2021-01-15 23:14
        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)
                })
    
    0 讨论(0)
  • 2021-01-15 23:16

    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);
    
    0 讨论(0)
  • 2021-01-15 23:16

    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
    
    0 讨论(0)
提交回复
热议问题