我有一個很大的jpeg圖像,我想在我的opengl引擎中異步加載tile。 如果它在主線程上完成但一切都很好,但速度很慢。在後臺線程中讀取CGImageRef使應用程序崩潰
當我嘗試將加載在NSOperationBlock上的圖塊加載時,它總是在嘗試訪問我以前在主線程中加載的共享圖像數據指針時崩潰。
必須有一些我不能與後臺操作,因爲我假設我可以訪問我在主線程上創建的內存部分。
我嘗試做的是以下幾點:
@interface MyViewer
{
}
@property (atomic, assign) CGImageRef imageRef;
@property (atomic, assign) CGDataProviderRef dataProvider;
@property (atomic, assign) int loadedTextures;
@end
...
- (void) loadAllTiles:(NSData*) imgData
{
queue = [[NSOperationQueue alloc] init];
//Loop for Total Number of Textures
self.dataProvider = CGDataProviderCreateWithData(NULL,[imgData bytes],[imgData length],0);
self.imageRef = CGImageCreateWithJPEGDataProvider(self.dataProvider, NULL, NO, kCGRenderingIntentDefault);
for (int i=0; i<tileCount; i++)
{
// I also tried this but without luck
//CGImageRetain(self.imageRef);
//CGDataProviderRetain(self.dataProvider);
NSBlockOperation *partsLoading = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakpartsLoadingOp = partsLoading;
[partsLoading addExecutionBlock:^{
TamTexture2D& pTex2D = viewer->getTile(i);
CGImageRef subImgRef = CGImageCreateWithImageInRect(self.imageRef, CGRectMake(pTex2D.left, pTex2D.top, pTex2D.width, pTex2D.height));
//!!!Its crashing here!!!
CFDataRef cgSubImgDataRef = CGDataProviderCopyData(CGImageGetDataProvider(subImgRef));
CGImageRelease(subImgRef);
...
}];
//Adding Parts loading on low priority thread. Is it all right ????
[partsLoading setThreadPriority:0.0];
[queue addOperation:partsLoading];
}
提示:UI更新必須在主運行循環來完成。您可以在後臺線程中執行處理,但在實際更新UI時強制執行主循環。 –
是的,我實際上在我的例子中抽象了這部分代碼,thx – AkademiksQc
你的一個參考是否爲NULL? – CodaFi