我有一个 iOS 应用程序,它有一个 UITableView 和包含 UIImageView 的自定义 TableViewCells。该图像是从 Web 服务加载的,因此在初始加载期间,我会显示“正在加载”图像,然后使用 gcd 进行分派(dispatch)并获取与该单元格数据匹配的图像。
当我使用 DISPATCH_QUEUE_PRIORITY_HIGH 全局队列执行图像提取时,我偶尔会在 tableview 单元格中加载错误的图像。如果我使用自己的自定义队列,则会将正确的图像填充到单元格中,但表格 View 的性能很糟糕。
这是代码...
// See if the icon is in the cache
if([self.photoCache objectForKey:[sample valueForKey:@"api_id"]]){
[[cell sampleIcon]setImage:[self.photoCache objectForKey:[sample valueForKey:@"api_id"]]];
}
else {
NSLog(@"Cache miss");
[cell.sampleIcon setImage:nil];
dispatch_queue_t cacheMissQueue = dispatch_queue_create("cacheMissQueue", NULL);
//dispatch_queue_t cacheMissQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(cacheMissQueue, ^{
if(sample.thumbnailFilename && sample.api_id){
NSData *thumbNailData = [[NSData alloc] initWithContentsOfFile:sample.thumbnailFilename];
UIImage *thumbNailImage = [[UIImage alloc]initWithData:thumbNailData];
if(thumbNailImage){
// Set the cell
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell sampleIcon]setImage:thumbNailImage];
[cell setNeedsLayout];
});
// save it to cache for future references
NSLog(@"DEBUG: Saving to cache %@ for sample %@",sample.thumbnailFilename,[sample objectID]);
[self.photoCache setObject:thumbNailImage forKey:sample.api_id];
}
}
});
dispatch_release(cacheMissQueue);
}
最佳答案
观看 WWDC 2012 session #211 帮助很大,我将代码从使用 GCD 更改为 NSOperationQueue,它解决了问题。
新代码...
[[self imgQueue]addOperationWithBlock:^{
if(sample.thumbnailFilename && sample.api_id){
NSData *thumbNailData = [[NSData alloc] initWithContentsOfFile:sample.thumbnailFilename];
UIImage *thumbNailImage = [[UIImage alloc]initWithData:thumbNailData];
if(thumbNailImage){
// Set the cell
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[[cell sampleIcon]setImage:thumbNailImage];
[cell setNeedsLayout];
}];
// save it to cache for future references
[self.photoCache setObject:thumbNailImage forKey:sample.api_id];
}
}
}];
关于ios - 调度队列上的 TableView 单元格中填充了错误的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800846/