gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:35:01 26 4
gpt4 key购买 nike

我遇到一个问题,每次调用表 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com