我有一个程序,我在其中设置了一个在后台线程上运行的完成块。在块内,我设置了一个 CGImageRef,然后在主线程上设置我的图层内容。问题是,有时应用程序在主线程部分崩溃。
这是完成块,在下面的代码中,fullImage 和 cfFullImage 都在我的 .h 中声明
requestCompleteBlock completionBlock = ^(id data)
{
// Seems I need to hold onto the data for use later
fullImage = (NSImage*)data;
NSRect fullSizeRect = NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height);
// Calling setContents with an NSImage is expensive because the image has to
// be pushed to the GPU first. So, pre-emptively push it to the GPU by getting
// a CGImage instead.
cgFullImage = [fullImage CGImageForProposedRect:&fullSizeRect context:nil hints:NULL];
// Rendering needs to happen on the main thread or else crashes will occur
[self performSelectorOnMainThread:@selector(displayFullSize) withObject:nil waitUntilDone:NO];
};
我的完成块的最后一行是调用 displayFullSize。该功能如下。
- (void)displayFullSize
{
[self setContents:(__bridge id)(cgFullImage)];
}
您是否看到或知道 setContents 可能失败的任何原因?
谢谢乔