【发布时间】:2016-02-21 11:18:16
【问题描述】:
我正在使用带有自定义单元格的表格视图。我想将图像存储在缓存中。
这是我的代码:
我的表格视图委托方法:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
LiveCameraCell* cell = ( LiveCameraCell* )[self.tableView dequeueReusableCellWithIdentifier:@"livecameracell"];
if (cell == nil) {
NSLog(@"Error dequeuing prototype cell");
}else{
cell.cameraData = [self getCamera:tableView forRowAtIndexPath:indexPath];
}
return cell;
}
- (CameraData*) getCamera:(UITableView*)tableView forRowAtIndexPath:(NSIndexPath*) indexPath {
CameraData* cameraData = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cameraData = (CameraData*) [searchResultsCameras objectAtIndex:indexPath.row];
} else {
cameraData = (CameraData*) [allCameras objectAtIndex:indexPath.row];
}
return cameraData;
}
-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
LiveCameraCell* liveCameraCell = (LiveCameraCell*)cell;
CameraData* cameraData = [self getCamera:tableView forRowAtIndexPath:indexPath];
liveCameraCell.mapButton.hidden = [cameraData hasGeoLocation] ? NO : YES;
if (![cameraData isSnapshotLoaded] && ![cameraData isLoadingImage]) {
/*[cameraData loadImageAsync1:^{
liveCameraCell.cameraImage.image = cameraData.snapshotImage;
[cell setNeedsLayout];
[cell setNeedsDisplay];
}];*/
[cameraData loadImageAsync1:^(UIImage* image) {
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(@"Image load");
if (![cameraData isThumbnailCachedWithHeight:[self thumbnailHeight] width:[self thumbnailWidth]])
{
if(cameraData.name == cameraData.name)
{
NSLog(@"Image load");
[liveCameraCell showThumbnail:cameraData.snapshotImage];
//liveCameraCell.cameraImage.image = cameraData.snapshotImage;
}
}
[cell setNeedsLayout];
[cell setNeedsDisplay];
});
}];
}
else
{
if (![cameraData isThumbnailCachedWithHeight:[self thumbnailHeight] width:[self thumbnailWidth]])
{
if (cameraData.snapshotImage !=liveCameraCell.cameraImage.image) {
liveCameraCell.cameraImage.image = cameraData.snapshotImage;
}
}
/*[cameraData downloadThumbnailWithHeight:[self thumbnailHeight] width:[self thumbnailWidth] callback:^(UIImage* thumbnail) {
dispatch_async(dispatch_get_main_queue(), ^{
[liveCameraCell showThumbnail:thumbnail];
});
}];*/
}
liveCameraCell.label.text = cameraData.name;
liveCameraCell.description.text = cameraData.description;
[cell setNeedsLayout];
[cell setNeedsDisplay];
}
所有工作都在我的willDisplayCell: 方法中完成。
这是我在willDisplayCell: 中使用的另一种方法:
-(void)loadImageAsync1:(void (^)(UIImage*))callback {
if (_snapshotImage) { callback(_snapshotImage); return; }
_loadingImage = YES;
HttpRequest* request = [HttpRequest requestWithRelativePath:[NSString stringWithFormat:@"/api/v1/camera/%@/snapshot?width=64", _uuid]];
[HttpResponse processAsynchronousRequest:request onCompletion:^(HttpResponse* response){
_snapshotImage = [UIImage imageWithData:response.responseData];
_loadingImage = NO;
if (!_snapshotImage) _snapshotImage = [UIImage imageNamed:@"gray_thumbnail_background.png"];
callback(_snapshotImage);
}];
}
请帮助集成这个库。我已经在我的项目中添加了类。
现在,下一步是什么?
如何在willDisplayCell:中添加这个?
提前致谢。
【问题讨论】:
-
使用 afnetworkings afnetworking+uiimageView 类,它提供了一种直接的方法来从 url 获取图像并将它们缓存在内存中......就像这样.. [imageView setImageforUrl:url placeholderImage:yourimage];跨度>
-
是的,我知道。但是如何在我的 willDisplayCell 中使用它?
-
因为我已经尝试过了。
-
不,我没有使用数据库。所以请告诉我如何在我的 willDisplayCell 方法中使用它?
-
目前只有第一次滚动后只有一个问题图像更改。是否有任何解决方案可以解决缓存问题,但主要问题是滚动时的图像变化?
标签: ios objective-c uitableview caching afnetworking