我已經使用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
不正確的意思是一些錯誤的位置隨機圖像。 –