作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个相当慢的 drawRect 方法(100-200 毫秒)。为了节省时间,我需要缓存结果。我正在像这样进行实际的缓存:
// some code to check if caching would be desirable goes here. If it is desirable, then
UIGraphicsBeginImageContext(viewSize);
CGContextRef c = UIGraphicsGetCurrentContext();
[view.layer renderInContext: c];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
[self.cachedImageArray addObject:image];
UIGraphicsEndImageContext();
缓存本身最多可能需要 40 毫秒。这还是很容易值得的。但是缓存必须等到所有内容都被渲染出来,否则会出错。此外,缓存是一项低优先级任务。一旦显示了所有内容,可能还有其他事情还在进行,如果是这样,缓存可以等待。但是因为它使用了 UIKit,所以它必须在主线程上。
有没有像这样等待的防弹方法,而不是任意延迟?
最佳答案
缓存本身不必在主线程上完成。您可以获得图像上下文或位图数据的副本/引用,并仅在渲染完成时使用 NSThread 启动它。示例:
- (void) drawRect:(CGRect)rect {
do_rendering_here();
// when rendering completed:
NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(doCaching:) object:c];
[t start];
[t release];
}
- (void) doCaching:(CGContextRef)ctx {
// do whatever kind of caching is needed
}
关于iphone - 在主线程上安排一个低优先级的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719204/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!