如何从 UIImagePickerController 将图像导入新的 VC?迅速
Posted
技术标签:
【中文标题】如何从 UIImagePickerController 将图像导入新的 VC?迅速【英文标题】:How to import a image to a new VC from UIImagePickerController? Swift 【发布时间】:2015-12-08 16:52:21 【问题描述】:我已经在我的应用程序中添加了一个相机和照片库,但我想要做的是当有人选择相机、拍照并选择它时,我希望它将用户引导到一个新的视图控制器并显示那里的图像而不是按钮所在的当前视图?
到目前为止,我还没有在谷歌上搜索,我已经设法在相机或库被关闭时加载新视图,但现在我似乎不知道如何将照片也导入新视图?!
如果有人能帮忙,那就太好了。
下面是我当前的代码:
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func Camera(sender: AnyObject)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
@IBAction func Images(sender: AnyObject)
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?)
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
self.presentViewController(svc, animated: true, completion: nil)
【问题讨论】:
【参考方案1】:根据上面的答案,您正在尝试将图像设置为图像视图,当您应该尝试将图像设置为图像视图的图像时,应该是:
func viewDidLoad() super.viewDidLoad()
// Because the self.image is an optional var, you need to unwrap it
if let image = self.image
self.imageView.image = self.image
【讨论】:
【参考方案2】:向视图控制器添加可选属性
class ImageViewController: UIViewController
var image : UIImage? = nil
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?)
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.image = image // here
self.presentViewController(svc, animated: true, completion: nil)
【讨论】:
您好,感谢您的回答。那 Class MyViewController... 是否与我的 ImageViewController 相对应? 我是把那个类位放在我上面展示给你的代码的同一个文件中还是放在第二个文件中? 转到 ImageViewController.swift 并在那里添加图像属性 我已经这样做了,但是当我运行它时,似乎没有什么不同。你输入“这里”的地方有什么建议去那里吗?【参考方案3】:在你的ImageViewController
,或者其他接收ViewController,添加:
var selectedImage: UIImage? = nil
在didFinishPickingImage
中,将您的代码替换为:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?)
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.selectedImage = image
self.presentViewController(svc, animated: true, completion: nil)
在svc
所指的接收ViewController中,添加显示图片的方法:
class ImageViewController: UIViewController
var selectedImage: UIImage? = nil
var yourImageView = UIImageView()
override func viewDidLoad()
super.viewDidLoad()
...
yourImageView.frame = view.frame
yourImageView.image = selectedImage
...
【讨论】:
【参考方案4】:我想建议一些修改。
请看这个答案:Presenting a modal view controller immediately after dismissing another
func presentImageViewerWithImage(image: UIImage)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.image = image //Need to make an UIImage instance variable in new viewController
self.presentViewController(svc, animated: true, completion: nil)
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?)
self.dismissViewControllerAnimated(false, completion:
self.presentImageViewerWithImage(image)
)
然后在 New View 控制器中创建一个UIImageView
IBOutlet
并将选定的图像表单选择器显示给那个'ImageView'
@IBOutlet weak var aImageView: UIImageView!
var image : UIImage?
override func viewDidLoad()
super.viewDidLoad()
if let image = image
self.aImageView.image = image
【讨论】:
以上是关于如何从 UIImagePickerController 将图像导入新的 VC?迅速的主要内容,如果未能解决你的问题,请参考以下文章