【发布时间】:2014-02-01 00:22:21
【问题描述】:
出于性能考虑,我想在静态内存空间中缓存 CGImageRef。
所以我有一个静态变量,它缓存了如下的图像内容,
UIImage *tempImage = [UIImage imageNamed:@"icon.jpg"];
static CGImageRef imageRef = nil;
if (!imageRef) {
imageRef = tempImage.CGImage;
}
在 UITableView 的委托方法中,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UIImageView *tempImageView = [[UIImageView alloc] .....
tempImageView.layer.contents = (__bridge id)imageRef;
[cell.contentView addSubview:tempImageView]; // added this image into UITableViewCell as a SubView.
}
这将在初始显示时起作用,
但是如果我选择了某个单元格,该单元格上的图像将在我选择后消失。(也许有一些内部绘制操作支持 UITableViewCell),我猜是选择事件使单元格进入高亮状态以进行显示。
没有下面的代码如何解决这个问题?我不想直接将一个UIImage分配给一个UIImageView。
tempImageView.image = tempImageView;
【问题讨论】:
-
如果你不想要
tempImageView.image = tempImageView;,请创建静态UIImageView -
谢谢,它会正常工作,但我想完全理解为什么在上述实现中选择后它会消失。
-
需要查看
cellForRowAtIndexPath的完整代码。您的行可能与未分配此图像的单元格一起重复使用。
标签: ios objective-c uitableview