gpt4 book ai didi

iphone - 回收父 ViewController 的 UIImage

转载 作者:行者123 更新时间:2023-11-28 22:52:39 25 4
gpt4 key购买 nike

我的 ViewController 创建了 60 个小的 UIView。每个都需要一个相同 jpg 的 UIImage(在 UIImageView 中使用)。

我的理论是,与其让每个 UIView 创建自己的 UIImage,不如重用一个在 ViewController 中定义的 UIImage。

View Controller 代码:

UIImage *reuseableUIImage = [UIImage imageNamed:@"LittlePicture.jpg"];

for (i=0; i<60; i++){
[arrayOfUIViews addObject:[[myUIViewMaker alloc] init...]];
}

我的理论错了吗?我应该继续在每个 UIView 中创建 UIImage 吗?

如果我的理论不错,我的 UIView 如何定位父 ViewController 的 UIImage?我不知道语法。为了说明(糟糕),UIView 中的代码将是类似的代码:

finalUIImageView = [[UIImageView alloc] initWithImage:self.parentViewController.reuseableUIImage];

最佳答案

您的代码看起来不错,应该可以正常工作。您需要在 viewController 上定义一个属性来保存 UIImage。

您将从中获得的主要好处是加载图像的时间只会发生一次。如果您为每个 View 分配和初始化图像,则每次都必须加载图像。

编辑:

再考虑一下,最好的方法是在初始化时将图像传递给 subview 。

UIImage *reuseableUIImage = [UIImage imageNamed:@"LittlePicture.jpg"];

for (i=0; i<60; i++){
[arrayOfUIViews addObject:[[myUIViewMaker alloc] initWithImage:reusableUIImage]];
}

myUIViewMaker 类中的 init 方法将实现为:

-(id)initWithImage:(UIImage *)image
{
self = [super init];
if (self) {
finalUIImageView = [[UIImageView alloc] initWithImage:image];
}
return self;
}

关于iphone - 回收父 ViewController 的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com