如何在不卡住的情况下在 TableView 单元格中显示 URL 个图像?
How to show URL Images in TableView cell without stuck?
我在 tableview
单元格中显示了 URLImage
的集合。当我滚动单元格时它被卡住了。我使用过 SDWebImageManager
和 AFNetWorking
等第三方。尽管如此,细胞还是被卡住了。
[imageVw setImageWithURL:[NSURL URLWithString:self.imgaeRecord.PreviewURl]placeholderImage:[UIImage imageNamed:@"default-thumbnail.jpg"]];
或
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:url
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
cell.recipeImageView.image = image;
}
}];
尝试在cellForRowAtIndexPath
方法中以这种方式使用SDWebImage
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
[cell.imageView sd_setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.recipeImageView.image = image
}];
在你的 cellForRowAtIndexPath 中
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:url
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
TableViewCellClass *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
updateCell.yourImageView.image = image;
});
}
}
}];
[task resume];
我认为当您必须执行许多图像请求操作时,您的问题仍然出在 tableview 的性能上。因此,为了优化请求,您应该取消当单元格离开 tableview 的可见区域时发出的请求。为此,在您的自定义单元格中实现 - (void)prepareForReuse
方法,并在其中假设您使用的是 SDWebImage,执行 [imageView sd_cancelCurrentImageLoad]
。此外,要下载图像并将其分配给 ImageView
,您只需执行
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]]
我在 tableview
单元格中显示了 URLImage
的集合。当我滚动单元格时它被卡住了。我使用过 SDWebImageManager
和 AFNetWorking
等第三方。尽管如此,细胞还是被卡住了。
[imageVw setImageWithURL:[NSURL URLWithString:self.imgaeRecord.PreviewURl]placeholderImage:[UIImage imageNamed:@"default-thumbnail.jpg"]];
或
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:url
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
cell.recipeImageView.image = image;
}
}];
尝试在cellForRowAtIndexPath
方法中以这种方式使用SDWebImage
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
[cell.imageView sd_setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.recipeImageView.image = image
}];
在你的 cellForRowAtIndexPath 中
NSURL *url =[NSURL URLWithString:[self.MoviesListImage objectAtIndex:indexPath.row]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:url
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
TableViewCellClass *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
updateCell.yourImageView.image = image;
});
}
}
}];
[task resume];
我认为当您必须执行许多图像请求操作时,您的问题仍然出在 tableview 的性能上。因此,为了优化请求,您应该取消当单元格离开 tableview 的可见区域时发出的请求。为此,在您的自定义单元格中实现 - (void)prepareForReuse
方法,并在其中假设您使用的是 SDWebImage,执行 [imageView sd_cancelCurrentImageLoad]
。此外,要下载图像并将其分配给 ImageView
,您只需执行
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]]