如何在我的应用程序中集成AFNetworking以获取缓存图像?

时间:2015-11-19 10:50:58

标签: ios objective-c uitableview caching afnetworking

我正在使用自定义单元格的表视图。我想将图像存储在缓存中 这是我的代码:
我的表格视图委托方法

 - (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:

中添加此内容

先谢谢。

2 个答案:

答案 0 :(得分:0)

根据编号将所有网址存储在数组中。细胞 然后在willDisplayCell方法就像那样

-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {

       [youCellImageView setImagewithUrl:[arrayUrl objectAtIndex:indexPath.row] placeholderImage:"placeHOlderImage"];
}

答案 1 :(得分:0)

您可以使用SDWebImage Library并使用以下方法下载图像。

[cell.imgProductImage setImageWithURL:[NSURL URLWithString:[[self.arrProduct objectAtIndex:indexPath.item] valueForKey:@"variant_image"]]  placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];