【问题标题】:Image Flickering everytime table is reloaded (iOS, objective-C)每次重新加载表时图像闪烁(iOS,objective-C)
【发布时间】:2016-03-17 20:15:51
【问题描述】:

每次调用 table 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];

}

【问题讨论】:

    标签: ios objective-c uitableview caching


    【解决方案1】:

    您可以使用 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");
    }];
    

    【讨论】:

    • 这不起作用。我使用的是按钮,而不是图像视图。
    • 没关系。我使按钮透明并将其放在下面。这很有效。谢谢
    • 欢迎@JoshO'Connor :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多