我有一个应用程序可以加载很多大图像。当我延迟加载它们时,即使在加载图像之后,单元格也不会加载它们,直到我将手指从屏幕上移开。我在方法中调用我的downloadImageForVisiblePaths
函数UIScrollViewDelegate
scrollViewDidEndDragging
中scrollViewDidEndDecelerating
除此之外,我还在UITableView
'scellForRowAtIndexPath
方法中设置图像,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Code to load reusable custom cell
CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath];
if(!object.mainImage){
[self downloadImageForIndexPath:indexPath];
cell.mainImageView.image = [UIImage imageNamed:@"placeholder"];
}else{
cell.mainImageView.image = object.mainImage;
}
return cell;
}
downloadImageForIndexPath
看起来像这样的地方:
-(void) downloadImageForIndexPath:(NSIndexPath*) indexPath{
UIImage *loadedImage = [[UIImage alloc] init];
// take url and download image with dispatch_async
// Once the load is done, the following is done
CustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.mainImageView.image = loadedImage;
CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath];
object.mainImage = loadedImage;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableVIew reloadData];
});
}
我看不出哪里出错了。即使手指在屏幕上,我也需要加载图像。这种行为类似于图像在 Google+、Instagram 或 Facebook 等应用上的加载方式。
任何指针将不胜感激。