我正在将数据从一个控制器传递到另一个控制器。

发送数据的TemplateController如下所示:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     PreviewController *destViewController = [[PreviewController alloc]init];
     destViewController.passFrameLabel = [self.frameImages[indexPath.section] objectAtIndex:indexPath.row];
     [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
     [destViewController loadFrame];
     [self dismissViewControllerAnimated:YES completion:nil];
}

我的loadFrame中的PreviewController方法如下所示:
UIImageView *frameImageView = [[UIImageView alloc] init];
[self.view addSubview:frameImageView];
self.frameImageView.image = [UIImage imageNamed:self.passFrameLabel];

调试后,self.passFrameLabel包含正确的值,但是self.frameImageView.image返回nil。我想念什么?

最佳答案

当您的类也具有frameImageView属性时,您似乎正在创建frameImageView本地对象。您正在将UIImageView的本地对象添加为子视图,但将image分配给该属性(似乎尚未初始化)。

您应该修改您的代码,

//frameImageView should be a strong property
_frameImageView = [[UIImageView alloc] init];
self.frameImageView.frame = CGRectMake(10,10,50,50); //Add suitable frame
//Now assign image
self.frameImageView.image = [UIImage imageNamed:self.passFrameLabel];
//Add this property as subview
[self.view addSubview:self.frameImageView];

试试看。

关于ios - UIImage返回零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20215031/

10-13 09:03