Ios 如何在TableView单元格中显示URL图像而不被卡住?
我已经在Ios 如何在TableView单元格中显示URL图像而不被卡住?,ios,objective-c,xcode,uitableview,nsurl,Ios,Objective C,Xcode,Uitableview,Nsurl,我已经在tableview单元格中显示了URLImage的集合。当我滚动单元格时,它被卡住了。我使用过第三方,如SDWebImageManager和AFNetWorking。电池一直被卡住 [imageVw setImageWithURL:[NSURL URLWithString:self.imgaeRecord.PreviewURl]placeholderImage:[UIImage imageNamed:@"default-thumbnail.jpg"]]; 或 NSURL *url =[
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;
}
}];
在
cellforrowatinexpath
method中尝试以这种方式使用SDWebImageNSURL *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
}];
在你的牢房里
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)prepareforeuse
方法,并在其中,假设您使用的是SDWebImage,请执行[imageView sd\u cancelCurrentImageLoad]
。此外,要下载图像并将其分配给ImageView
,只需执行以下操作
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]]
标题的意思是“卡住”吗?如果这是您的意思,那么“卡住”是什么意思?您使用的是默认单元格还是自定义单元格?在图像下载完成后,检查它是否是主线程:NSThread.isMainThread()。如果不是,则跳转到主线程并更新UI:dispatch\u async(dispatch\u get\u main\u queue(),{//cell.recipeImageView.image=image;})@danh Stick表示在滚动单元格时表格视图被阻塞。@tx2这里我使用了自定义单元格(HorizontalTableViewCell)。用于水平显示单元格。