如何从UIImagePickerController获取图像并传递给下一个VC

时间:2015-06-01 13:15:00

标签: ios swift

我从PhotoLibrary中选择一张照片,如何实现以下任务

在这种情况下,我使用的是Swift。我需要通过以下方式重建下一个VC中的图像:
a)字节
b)图像

通过使用Segue或用户类。如果图像文件很大,最好通过segue或class。

1)从UIImagePickerController获取图像或字节

2)如何将图像字节传递给下一个VC
   使用Segue
   或使用班级    我是否使用用户类来存储图像或字节?

3)如何获得高度和宽度,以便我可以知道图像是纵向还是横向模式。

4)如何检查ImageView中是否有图像?

在导航到下一个VC之前,我有一个btn点击检查

非常感谢您的帮助

以下是我使用的代码:

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

6 个答案:

答案 0 :(得分:3)

  1. 您需要将委托分配给图像选择器,以便调用适当的方法。在loadImageButtonTapped函数中,不要忘记指定图像选择器代理。
  2. 然后,您应该实现此方法:

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

    如果你需要找出图像的大小,你可以访问它的size属性。

    1. 要将图像传递给另一个视图控制器,您可以使用以下两个选项之一:

      • 您可以使用delegation
      • 当您使用storyboard segues从视图控制器导航到另一个控制器时,您可以使用此功能:

        func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) 
        {
             var destinationController = segue.destinationViewController
             // now you can pass the image to the destination view controller
        }
        
    2. P.S: didFinishPickingImage 自iOS 3以来已被弃用,您不应使用该方法。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

1)从UIImagePickerController获取图像

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

2)将图像传递给其他ViewController在第二个VC中创建UIimage的属性并为其分配图像对象。

secondVC.imageProperty = image;

3)获取图像的高度和宽度

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

答案 3 :(得分:1)

对于 Swift 4.2

使用以下

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

}

答案 4 :(得分:0)

改为使用imagePickerController didFinishPickingImage:

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

答案 5 :(得分:0)

尝试

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