gpt4 book ai didi

iphone - Grand Central Dispatch - 加载时显示第一张图片?

转载 作者:可可西里 更新时间:2023-11-01 04:42:29 26 4
gpt4 key购买 nike

我只想预渲染不同的图像以便快速访问。我在这里使用 grand central dispatch 来执行不同的 block 。

启动队列后,我想在完成后设置第一张图像。不幸的是,使用下面的当前代码,只有在渲染完所有图像后才会显示第一张图像。

那么我该如何修改代码呢?每张图片完成后是否有可能获得代表?

代码如下:

// 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;

}

我只想知道如何取消/停止或重新启动正在运行的队列?那可能吗?感谢您的帮助。

最佳答案

你必须使用串行队列,例如执行 FIFO:

dispatch_queue_t queue;
queue = dispatch_queue_create("myImageQueue", NULL);
for(int i = 0; i<count; i++) {
dispatch_async(queue, ^{
// do your stuff in the right order
});
}

对于串行调度队列,请查看: http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html

关于iphone - Grand Central Dispatch - 加载时显示第一张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10450923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com