在我的应用程序中,我希望将大量(相同的PNG)文件堆叠在一起,并向用户显示图像结果以及进度条。为了提高性能,我提出了以下代码,但只有当for循环结束时,我才会看到最终结果。 对于大型图像集,这不是我想要的,我真的希望看到中间结果。以下代码可能包含我无法找到的错误:
- (IBAction)doDrawLayers:(NSArray *)drawImages
{
// Use dispath-queues for drawing intermediate result and progress.
_startDrawing = [NSDate date];
dispatch_queue_t calcImage = dispatch_queue_create("calcImage", NULL);
dispatch_async(calcImage, ^{
_sldProgress.maximumValue = drawImages.count;
_sldProgress.minimumValue = 0;
_imageNr = 0;
[_sldProgress setMaximumValue:drawImages.count];
for (NSString *imageFile in drawImages) {
@autoreleasepool {
// Update in main queue.
dispatch_async(dispatch_get_main_queue(), ^{
UIGraphicsBeginImageContext(_imageSize);
// Show progress update.
_lblProgress.text = [NSString stringWithFormat:@"%d %.2f sec", ++_imageNr, -[_startDrawing timeIntervalSinceNow]];
_sldProgress.value = _imageNr;
NSString *filePath = [[NSBundle mainBundle] pathForResource:imageFile ofType:nil];
[[UIImage imageWithContentsOfFile:filePath] drawAtPoint:CGPointMake(0., 0.)];
// Get CGImage from the offscreen image context.
_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
}
}
});
// Finished calculating image, release dispatch.
dispatch_release(calcImage);
}
我也在努力解决UIGraphicsBeginImageContext / UIGraphicsEndImageContext对的最佳位置问题,因为我想最大限度地减少使用的内存量并最大限度地提高整体性能。
答案 0 :(得分:0)
最后通过合并来自Apple的PhotoScroller(主要基于CATiledLayer)和LargeImageDownsizing示例代码的代码找到了解决方案。