高速アクセスのために、さまざまな画像を事前にレンダリングしたいだけです。ここでは、グランドセントラルディスパッチを使用してさまざまなブロックを実行します。
キューを開始した後、完了したら最初のイメージを設定したいと思います。以下の現在のコードでは、残念ながら、最初の画像はすべての画像がレンダリングされたときにのみ表示されます。
では、どうすればコードを変更できますか?各画像が終了したときに代理人を取得することは可能ですか?
コードは次のとおりです。
// Async prerendering
for (int i = 0; i < count; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
UIImage* finalImage = [self prerenderImageForIndex:i];
[self.imageArray addObject:finalImage];
// TODO: i want to display the first image.
// rendering goes on in the background
if (i==0 && [self.imageArray objectAtIndex:0] != nil ) {
self.image = [self.imageArray objectAtIndex:0];
}
});
});
}
アップデート:
-(UIImage*) prerenderImageForIndex:(int)frame {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width, self.frame.size.height), NO, 0);
for (int i=0; i< [configurationArray count]; i++) {
//... get the layerName
UIImage* layerImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:layerName ofType:@"png"]];
// draw layer with blendmode and alpha
[layerImage drawInRect:CGRectMake(x, y, layerImage.size.width, layerImage.size.height)
blendMode:layerblendmode
alpha: layeralpha];
}
// Get current context as an UIImage
UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
実行中のキューをキャンセル/停止または再開する方法を知りたいだけですか?それは可能ですか?ご協力いただきありがとうございます。