我只是想预渲染不同的图像以便快速访问。 我在这里使用大中央调度执行不同的块。Grand Central Dispatch - 在加载时显示第一张图片?
启动队列后,我想在完成后设置第一个图像。 使用下面的当前代码,不幸的是,第一个图像将只显示所有图像已被渲染。
那么我如何修改代码?每张图片完成后是否有可能获得委托?
Here's代码:
// 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;
}
我只是想知道如何取消/停止或重新启动正在运行的队列? 这可能吗? 感谢您的帮助。
是的,这就解决了问题!非常感谢你。我在“dispatch_async(queue,^ {”)中分配了“self.image”,现在它工作的很完美,不过我会看看关于这个的文档,再次感谢。 – geforce