Tôi chỉ muốn hiển thị trước các hình ảnh khác nhau để truy cập nhanh. Tôi sử dụng công văn trung tâm lớn ở đây để thực hiện các khối khác nhau.Grand Central Dispatch - hiển thị hình ảnh đầu tiên khi nó được tải?
Sau khi bắt đầu hàng đợi, tôi muốn đặt hình ảnh đầu tiên khi thực hiện xong. Với mã hiện tại bên dưới, rất tiếc, hình ảnh đầu tiên sẽ chỉ được hiển thị khi tất cả hình ảnh đã được hiển thị.
Vậy làm cách nào tôi có thể sửa đổi mã? Có thể nhận được một đại biểu khi mỗi hình ảnh đã hoàn thành?
Here's mã:
// 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];
}
});
});
}
Cập nhật:
-(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;
}
tôi chỉ muốn biết làm thế nào để hủy/dừng hoặc khởi động lại một hàng đợi chạy? Có thể không? Cảm ơn sự giúp đỡ của bạn.
Vâng, điều đó giải quyết được vấn đề! Cảm ơn nhiều. Tôi gán "self.image" trong "dispatch_async" (hàng đợi,^{". Bây giờ nó hoạt động hoàn hảo! Tuy nhiên tôi sẽ có một cái nhìn tại các tài liệu về điều đó. Cảm ơn một lần nữa. – geforce