在后台线程读CGImageRef崩溃的应用程序

我有一个很大的JPEG图像,我想在我的opengl引擎asynchronous加载瓷砖。 如果它在主线程上完成,但一切都很好,但速度很慢。

当我试图把加载在一个NSOperationBlock的瓷砖,它总是崩溃时,试图访问我以前加载在主线程上的共享图像数据指针。

必须有一些我不能与后台操作,因为我假设我可以访问我在主线程上创build的内存部分。

我试图做的是以下几点:

@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]; } 

我终于find了我的问题…

我已经阅读了Quartz2D文档,似乎我们不应该再使用CGDataProviderCreateWithData和CGImageCreateWithJPEGDataProvider。 我想那里用法不是线程安全的。

正如文档build议,我现在使用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);