如何从UIImagePickerController获取图像并传递给下一个VC [英] How to get image from UIImagePickerController and Pass to next VC

查看:115
本文介绍了如何从UIImagePickerController获取图像并传递给下一个VC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

a)字节

b)图像

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

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

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

1)从UIImagePickerController获取图像或字节

1) Get the Image or Bytes from the UIImagePickerController

2)如何将图像字节传递给下一个VC

使用Segue

或使用类

我是否使用用户类来存储图像或字节?

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)如何获得高度和宽度,以便我可以知道图像是纵向还是横向模式。

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

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

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

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


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

非常感谢您的帮助

以下是我使用的代码:


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


推荐答案


  1. 您需要将代理人分配给您的图像选择器这样就可以调用适当的方法。在 loadImageButtonTapped 函数中,不要忘记指定图像选择器委托。

  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: [NSObject : AnyObject]){ 
        let image = info[UIImagePickerControllerOriginalImage] 
    }

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

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


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

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


  • 你可以使用委派

  • 您可以在从视图控件导航时使用此功能呃另一个使用storyboard segues:

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


PS: didFinishPickingImage 自iOS 3起已弃用,您不应使用该方法。

这篇关于如何从UIImagePickerController获取图像并传递给下一个VC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆