ios - 每次重新加载表格时图像闪烁(iOS,objective-C)

标签 ios objective-c uitableview caching

我遇到一个问题,每次调用表 reloadData 方法时,我的 tableview 单元格中的图像都会闪烁。发生闪烁是因为每次重新加载表格时都会下载图像。我如何才能让这张图片不是每次都被下载,而是只被下载一次?

这是 SelectStationViewController.m 中的 cellForRowAtIndexPath 代码。此类处理 tableView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    StationRest * StationRest = [[CurrentUser sharedInstance].userStations objectAtIndex:indexPath.row];
    StationListCell *cell= [[StationListCell alloc]initWithFrame:CGRectMake(0, 0, 375,88)];
    cell.cellDelegate = self;

    //This method below downloads the image into the cell.
    [cell configureCellWithStationRest:StationRest forCellType:StationListCellTypeSelect];

    return cell;
}

这是 StationListCell.m 中的代码,与单元连接的类。这是使用 AFNetworking 下载图像的位置。我可以将 GCD 与 [[NSData alloc] initWithContentsOfURL 方法而不是 AFNetworking 一起使用,但我仍然获得相同的结果。

-(void)configureCellWithStationRest:(StationRest *)stationRest forCellType:(StationListCellType) cellType{

    NSURL *url = [NSURL URLWithString:stationRest.thumbURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.thumbButton setImage:responseObject forState:UIControlStateNormal];
        [self.thumbButton setContentMode:UIViewContentModeScaleAspectFill];
        } 
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            DLog(@"Image error: %@", error);
        }];

    [requestOperation start];

}

最佳答案

你可以使用UIImageView+AFNetworking.h类。它提供了从url下载图片并缓存它的方法。更多信息请查看它的文档。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
[yourImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    yourImageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Failed to load image");
}];

关于ios - 每次重新加载表格时图像闪烁(iOS,objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231636/

相关文章:

ios - 按钮自定义单元格

ios - 元素在 UItableviewCell 子类之后不显示

ios - 如何在ios swift中从Parse中按降序对NSDate进行排序

ios - Physicsbody 不遵守节点的 anchor

ios - 相册像表 iOS

iphone - 释放 Controller ,但保留空指针?

ios - 如何在不更改文本的 alpha 的情况下更改 TableViewCell 的 alpha?

ios - UILabel 不会在 UITableViewCell 中的初始加载时换行

ios - 自定义附件 View 按钮的操作是什么 - swift

iOS 如何复制 Youtube App 播放器旋转?