问题描述
我正在尝试在下一个视图控制器上加载在 UIImagePickerController
上选择的图片.我在类似的线程上进行了一些搜索,并找到了一些帮助我设置它的线程,但是图像并没有真正转移"到下一个视图控制器.
I am trying to load a picture selected on UIImagePickerController
on the next view controller. i did some search on similar threads and found a few that helped me set it up, but the image does not really get 'transfered' to the next view controller.
这是我的 didFinishPickingImage 和 prepareForSegue 代码:
this is my code for the didFinishPickingImage and prepareForSegue:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.imageChosen = image;
[picker dismissModalViewControllerAnimated:NO];
[self performSegueWithIdentifier:@"Edit" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.imageView.image = self.imageChosen;
}
我正在运行到 EditViewController
的 segue,但是那里的 imageView 没有加载图片.我猜这个作业是错误的,但我看不出是怎么回事.
I am getting the segue running to the EditViewController
, but the imageView there doesnt load the picture. I guess the assignment is wrong somehow but I fail to see how.
推荐答案
当您尝试将图像设置为 EditViewController
UIImageView
它不存在,因为 EditViewController
尚未加载.而不是将图像设置为 UIImageView
直接在 EditViewController
中创建一个实例变量来保存图像.然后将此图像分配给 viewDidLoad:
When you try to set image to EditViewController
UIImageView
it doesn't not exist because EditViewController
not loaded yet. Instead of setting image to UIImageView
directly create an instance variable in EditViewController
that will hold image. And than assign this image to UIImageView
in viewDidLoad:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.image = self.imageChosen;
}
//EditViewController.h
...
@property (nonatomic, strong) UIImage *image;
...
//EditViewController.m
- (void)viewDidLoad {
...
self.imageView.image = self.image;
...
}
这篇关于转场后无法在下一个 ViewController 上从 UIImagePickerController 加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!