我正在尝试在下一个视图控制器上加载UIImagePickerController
上选择的图片。我对类似的线程进行了一些搜索,发现了一些帮助我设置的线程,但图像并没有真正“转移”到下一个视图控制器。
这是我的didFinishPickingImage和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;
}
我让segue运行到EditViewController
,但是imageView没有加载图片。我猜这个任务在某种程度上是错误的,但我没有看到如何。
答案 0 :(得分:1)
当您尝试将图片设置为EditViewController
UIImageView
时,它不存在,因为EditViewController
尚未加载。而不是将图像设置为UIImageView
直接在EditViewController
中创建将保存图像的实例变量。而不是将此图片分配到UIImageView
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;
...
}