我已经使用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"];
}
不正确=错误的位置或只是noImageThumb而不是实际的图像? – Shubhank
不正确的意思是一些错误的位置随机图像。 –