UIImage 没有从一个 ViewController 传递到另一个
UIImage not passing from one ViewController to other
我正在尝试将从 UIImagePickerController
捕获的图像传递给另一个 ViewController
,但图像没有传递。
这是第一个ViewController
的代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
self.performSegue(withIdentifier: "pass", sender: self)
dismiss(animated:true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
present(svc, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pass"
{
let imgpass = segue.destination as! demoViewController
imgpass.newphoto = chosenImage
}
}
第二个代码 ViewController
:
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = newphoto
}
我浏览了很多其他类似的帖子并实现了它们,但无法通过图像。
试试这个
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
picker.dismiss(animated: true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
svc.newphoto = chosenImage
self.present(svc, animated: true, completion: nil)
}
}
问题是您的 func prepare(for:sender:)
实现从未被调用,因此您的 newphoto
从未被设置。 prepare(for:sender:)
在 segue 之前发送。但是没有segue。您在代码 中呈现 ,而不是使用 segue。
我正在尝试将从 UIImagePickerController
捕获的图像传递给另一个 ViewController
,但图像没有传递。
这是第一个ViewController
的代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
self.performSegue(withIdentifier: "pass", sender: self)
dismiss(animated:true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
present(svc, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pass"
{
let imgpass = segue.destination as! demoViewController
imgpass.newphoto = chosenImage
}
}
第二个代码 ViewController
:
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = newphoto
}
我浏览了很多其他类似的帖子并实现了它们,但无法通过图像。
试试这个
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
picker.dismiss(animated: true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
svc.newphoto = chosenImage
self.present(svc, animated: true, completion: nil)
}
}
问题是您的 func prepare(for:sender:)
实现从未被调用,因此您的 newphoto
从未被设置。 prepare(for:sender:)
在 segue 之前发送。但是没有segue。您在代码 中呈现 ,而不是使用 segue。