Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在我的应用程序中为缓存图像集成网络?_Ios_Objective C_Uitableview_Caching_Afnetworking - Fatal编程技术网

Ios 如何在我的应用程序中为缓存图像集成网络?

Ios 如何在我的应用程序中为缓存图像集成网络?,ios,objective-c,uitableview,caching,afnetworking,Ios,Objective C,Uitableview,Caching,Afnetworking,我正在使用带有自定义单元格的表视图。我想在缓存中存储图像。 这是我的代码: 我的表视图委托方法: - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { LiveCameraCell* cell = ( LiveCameraCell* )[self.tableView dequeueReusableCellWithIdentifier:@"

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

 - (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:
中添加此项


提前感谢。

根据单元格数量将所有url存储在一个数组中 然后在willDisplayCell方法中就是这样做的

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

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

您可以使用SDWebImage库,并使用以下方法下载图像

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

使用afnetworkings afnetworking+uiimageView类,该类提供了从url获取图像并将其缓存在内存中的直接方法。。。这样地。。[imageView setImageforUrl:url占位符图像:yourimage];是的,我知道。但如何在我的willDisplayCell中使用它?因为我已经尝试过了。不,我没有使用数据库。所以请告诉我如何在我的willDisplayCell方法中使用这个?目前只有一个问题,第一次滚动后图像会发生变化。是否有任何解决方案,缓存正在工作,但主要问题是在滚动时更改图像?