gpt4 book ai didi

ios - Collection View 单元格中的图像不正确

转载 作者:行者123 更新时间:2023-11-29 00:52:40 27 4
gpt4 key购买 nike

我使用 sdwebimage 来缓存图像。现在 imagesArr 是我想首先为所有单元格显示的图像数组。我在后台下载图像,然后保存到磁盘中,所以当我想要离线模式下的这些图像时,我可以从磁盘中获取。

但在某些情况下,当我滚动 collectionView 时图像设置不正确,然后再次滚动图像似乎是正确的。

这是我在 cellForItemAtIndexPath 中实现的以下代码。我已尝试调试,但似乎数据正确,图像名称也正确。

此外,collectionView 在滚动时滞后。请帮我找出问题所在。

 if(imagesArr.count>0)
{
ClothesImage *obj_image=[imagesArr objectAtIndex:0];
NSString *fileName = [stringPath stringByAppendingFormat:@"/%@",obj_image.imagePath];
NSLog(@"FILES NAMES %@ ", obj_image.imagePath);
NSData *data = [NSData dataWithContentsOfFile:fileName];
if(data!=nil){
UIImage *image=[UIImage imageWithData:data];
cell.imgProduct.image=image;
}
else{
NSString *offline=[user_defaults objectForKey:@"offline"];

if([offline integerValue]==0){
cell.imgProduct.image=[UIImage imageNamed:@"noImageThumb"];
NSString *str_url=[NSString stringWithFormat:@"%@/%@",WEB_THUMB_IMAGE,obj_image.imagePath];
str_url = [NSString stringWithFormat: @"%@",[str_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[cell.imgProduct setImageWithURL:[NSURL URLWithString:str_url] placeholderImage:[UIImage imageNamed:@"noImageThumb"] options: SDWebImageRefreshCached];

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:str_url] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
NSString *stringPathImage = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"WebThumb"];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
NSString *fileName = [stringPathImage stringByAppendingFormat:@"/%@",obj_image.imagePath];
[data writeToFile:fileName atomically:YES];
NSLog(@"DOWNLOADED IMAGE %@",fileName);

}];
}
else
{
cell.imgProduct.image=[UIImage imageNamed:@"noImageThumb"];
}
}
} else{
cell.imgProduct.image=[UIImage imageNamed:@"noImageThumb"];
}

最佳答案

我认为有几种方法可以改进这一点。

看起来您正在重复 SDWebImage 将为您做的很多工作。该库将缓存图像并按需获取它们。您应该考虑让该库将您的文件预取到缓存中,而不是您自己尝试预取并将它们写出到磁盘。最终,当您进入 -cellForRowAtIndexPath 时,您只需要两种情况:一种用于在设备离线时设置图像,另一种从缓存中获取或设置图像,其中 SDWebImage为您提供:

    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

就是这样。您所做的所有缓存和切换都不是必需的。另一件要确定的事情是在你的单元格子类方法 -prepareForReuse 中,你正在消除图像。

关于ios - Collection View 单元格中的图像不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37947012/

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