私のアプリでは、多数の(同一の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 ペアを配置するのに最適な場所は何かという問題にも苦労しています。