gpt4 book ai didi

iphone - 在主线程上安排一个低优先级的任务

转载 作者:可可西里 更新时间:2023-11-01 06:12:17 26 4
gpt4 key购买 nike

我有一个相当慢的 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/

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