ios 将图像传递给另一个视图控制器 (Swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31576232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Image to another View Controller (Swift)
提问by vacawama
I'm trying to move a user uploaded image from one UIImageViewto another UIImageViewon a different View Controller. I'm able to have the user upload an image and have it saved in the first UIImageViewbut after they press the "Post" button, the UIImageView on the next view controller remains blank.
我正在尝试将用户上传的图像从一个UIImageView 移动到另一个视图控制器上的另一个UIImageView。我可以让用户上传图像并将其保存在第一个UIImageView 中,但是在他们按下“发布”按钮后,下一个视图控制器上的 UIImageView 保持空白。
Note: browsingImage is the name of the UIImageView on the second view controller (destination UIImageView)
注意:browsingImage 是第二个视图控制器(目标 UIImageView)上的 UIImageView 的名称
Any help would be greatly appreciated!
任何帮助将不胜感激!
@IBAction func cameraButton(sender: AnyObject) {
addNewPicture()
}
func addNewPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
postingImage.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func postButton(sender: AnyObject) {
performSegueWithIdentifier("toBrowsePage", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.postingImage.image = browsingImage.image
}
}
回答by vacawama
In prepare(for:)
you can't access the @IBOutlet
s of the destination view controller because they haven't been set up yet. You should assign the image to a property of the destination view controller, and then move it into place in viewDidLoad()
:
在prepare(for:)
您无法访问@IBOutlet
目标视图控制器是因为它们还没有被建立呢。您应该将图像分配给目标视图控制器的属性,然后将其移动到以下位置viewDidLoad()
:
In source view controller:
在源视图控制器中:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toBrowsePage" {
let dvc = segue.destination as! ListPage
dvc.newImage = postingImage.image
}
}
In destination view controller:
在目标视图控制器中:
class ListPage: UIViewController {
@IBOutlet weak var browsingImage: UIImageView!
var newImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
browsingImage.image = newImage
}
}
回答by Leo
From your description,browsingImage
is in destination viewController,so
in this line
根据您的描述,browsingImage
在目标视图控制器中,所以在这一行
itemToAdd.postingImage.image = browsingImage.image
You pass the destination imageview to source imageview
您将目标图像视图传递给源图像视图
回答by iPhoneDeveloper
IBOutlets are not accessible in your current view controller. The outlets are not yet initialized since the your second view is not yet loaded.
在您当前的视图控制器中无法访问 IBOutlets。出口尚未初始化,因为您的第二个视图尚未加载。
Instead you can create a UIImage variable in your second viewController and assign the value to it in the prepareForSegue.
相反,您可以在第二个 viewController 中创建一个 UIImage 变量,并在 prepareForSegue 中为其分配值。
And in your second viewController's viewDidLoad method assign that value to your ImageView outlet.
并在您的第二个 viewController 的 viewDidLoad 方法中将该值分配给您的 ImageView 插座。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
let destination = segue.destinationViewController as! ListPage
destination.receivedImage = postingImage.image
}
}
In your next viewController declare a variable called
在您的下一个 viewController 中声明一个名为的变量
@IBOutlet weak var postingImage: UIImageView!
var receivedImage : UIImage?
In the viewDidLoad method
在 viewDidLoad 方法中
postingImage.image = receivedImage
回答by Qazi
At the time of the Segue
happening your UIImageView is not initialised so you can not assign an image to it, the best practise would be to pass the UIImage
and initialise your UIImageView
in viewDidLoad
.
在Segue
发生时,您的 UIImageView 未初始化,因此您无法为其分配图像,最佳做法是传递UIImage
并初始化您的UIImageView
in viewDidLoad
.
so in your ListPage
class make a property of type UIImage
change your prepareForSegue
line as
所以在你的ListPage
课上做一个类型的属性UIImage
改变你的prepareForSegue
行
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.image = browsingImage.image
}
}
in viewDidLoad
or viewDidAppear
of your destination viewController
you will do something like this
在你的目的地viewDidLoad
或viewDidAppear
在你的目的地,viewController
你会做这样的事情
browsingImage.image = image