2013-05-21 85 views
2

我有一個很大的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]; 

} 
+0

提示:UI更新必須在主運行循環來完成。您可以在後臺線程中執行處理,但在實際更新UI時強制執行主循環。 –

+0

是的,我實際上在我的例子中抽象了這部分代碼,thx – AkademiksQc

+0

你的一個參考是否爲NULL? – CodaFi

回答

2

我終於找到了我的問題......

我已閱讀Quartz2D doc和我們似乎不應該使用CGDataProviderCreateWithData和CGImageCreateWithJPEGDataProvider了。我想那裏的用法不是線程安全的。

正如所建議的醫生,我現在用的CGImageSource API這樣的:

self.imageSrcRef = CGImageSourceCreateWithData((__bridge CFDataRef)imgData, NULL); 

// get imagePropertiesDictionary 
CFDictionaryRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(m_imageSrcRef,0, NULL); 

self.imageRef = CGImageSourceCreateImageAtIndex(m_imageSrcRef, 0, imagePropertiesDictionary); 
+0

我正在運行與CGImageCreateWithJPEGDataProvider無關的線程問題。但是我找不到表示它不是線程安全的Apple文檔,您指的是。你還能找到它嗎? – iljawascoding

+0

我沒有閱讀CGImageCreateWithJPEGDataProvider不是線程安全的任何地方,它只是我在測試後做出的一個結論...除非我做錯了什麼,但我看不到...只是切換到ImageIO解決了我的問題 – AkademiksQc

相關問題