未能在赛格瑞后下一个视图控制器从加载的UIImagePickerController图像(Fails

2019-10-17 19:13发布

我试图加载所选的图片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的有犯规加载图片。 我猜的分配是错误的莫名其妙,但我看不出。

Answer 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;
    ...
}


文章来源: Fails to load image from UIImagePickerController on next ViewController after segue