我有一个带UIImageView
字段的 tableView。我在从settersetupCell
调用的方法中设置它。set_book
Setter 从 调用tableView:willDisplayCell:forRowAtIndexPath:
。这是 setupCell 的一部分:
dispatch_queue_t downloadImage = dispatch_queue_create("Download Image", NULL);
dispatch_async(downloadImage, ^{
NSString* key = [_book.internalID stringByAppendingPathExtension:@"thumb"];
UIImage* thumb = [[[LTImagesCacheController shared] cache] objectForKey:key];
if (thumb == nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
_bookImageView.layer.borderWidth = 0.0;
_bookImageView.image = [[LTImageProvider sharedImagesProvider] imageForName:@"DefaultBook.png" cache:YES];
});
NSString* imageName = [[LTImagesCacheController cacheDir] stringByAppendingPathComponent:key];
if([[NSFileManager defaultManager] fileExistsAtPath:imageName])
{
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], NSFileModificationDate, nil];
[[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:imageName error:nil];
thumb = [UIImage imageWithContentsOfFile:imageName];
[[[LTImagesCacheController shared] cache] setObject:thumb forKey:key];
}
else
{
NSString* url = _book.coverURL;
thumb = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
if (thumb != nil)
[[[LTImagesCacheController shared] cache] setObject:thumb forKey:key];
}
}
if (thumb != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
_bookImageView.layer.borderWidth = 1.0;
_bookImageView.image = thumb;
});
}
});
dispatch_release(downloadImage);
当我运行我的应用程序时,有时我会在短时间内冻结 UI,不到 1 秒。我做错了什么?我想将 DefaultBook.png 设置为默认值,如果存在 _book.coverURL 中的图像,请设置它。我有大约 100-200 个单元格,拇指图像大小为 15-30 KB。