gpt4 book ai didi

ios - 如何将选定的图像从 UIImagePickerController 传递到第二个 View Controller ?

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:48 26 4
gpt4 key购买 nike

我竭尽全力寻找这个问题的答案。在我正在创建的应用程序中,用户将单击 addImage 按钮,然后从 ImagePickerController 的照片库(或相机)中选择一个图像,然后将所选图像传递到第二个 View Controller 上的 ImageView 。

我知道当 ImageView 与 addImage 按钮位于同一 View 时如何执行此操作,但无法弄清楚如何正确使用 segue 将图像传递到第二个 View 。希望有人能帮我解决这个问题。

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

var newImage = UIImage(named: "")


@IBOutlet var imageView: UIImageView!

var imagePicker = UIImagePickerController()

override func viewDidLoad() {
super.viewDidLoad()

imagePicker.delegate = self

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func addImage(sender: UIButton) {

var alert = UIAlertController(title: "Please Get An Image", message: "Choose from below", preferredStyle: .Alert)

alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: {(action) -> Void in

self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {(action) -> Void in

self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: {(action) -> Void in

self.dismissViewControllerAnimated(true, completion: nil)



}))


self.presentViewController(alert, animated: true, completion: nil)

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

newImage = image

//not sure about this next line here...
performSegueWithIdentifier("VC2", sender: self)


self.dismissViewControllerAnimated(true, completion: nil)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

var secondViewController: VC2 = segue.destinationViewController as! VC2
secondViewController.imageView.image = newImage




}


}// end of app

最佳答案

你不应该使用 UIImageView 对象传递 UIImage 因为这样传递图像不是一个好主意,相反你可以在 SecondViewController 中创建 UIImage 对象简单地将 UIImage 对象传递给它,然后在 SecondVC 中像这样使用这个图像对象

override func viewDidLoad() {
super.viewDidLoad()
if imageView != nil {
imageView.image = newImage
}
}

关于ios - 如何将选定的图像从 UIImagePickerController 传递到第二个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31995868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com