1

我正在尝试加载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 没有加载图片。我猜这个任务在某种程度上是错误的,但我不知道怎么做。

4

1 回答 1

1

当您尝试将图像设置为EditViewController UIImageView它不存在时,因为EditViewController尚未加载。而不是设置图像UIImageView直接创建一个实例变量EditViewController,将保存图像。而不是将此图像分配UIImageViewviewDidLoad:

- (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;
    ...
}
于 2013-03-09T13:07:22.327 回答