I am trying to integrate UICollectionView with multiple images. I am fetching images from API dynamically and loading directly it into the UICollectionViewCell by using the SDWebImage library. Its working fine for me. But now I am trying to load downloaded images from document directory into the UICollectionViewCell, it shows image in wrong cell. Each time I load UICollectionViewCell or scroll it images get changed in cell
-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url{
NSString *workSpacePath=[[Utility applicationDocumentsDirectory] stringByAppendingPathComponent:strImageName];
BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:workSpacePath];
if (fileExists) {
imgView.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
}
else{
[imgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default1"]];
}
}
When I load images only from server directly it works fine for me. But I want to check if image file is present my document directory or not. If YES then load it from document directory otherwise load it from server. Please provide your valuable suggestions.
Here I am setting the cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell"
forIndexPath:indexPath];
UILabel *recipeNameLabel = (UILabel *)[myCell viewWithTag:200];
recipeNameLabel.text = [[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@“name”];
UIImageView *imgSpace = (UIImageView *)[myCell viewWithTag:100];
NSString *strURL = [NSString stringWithFormat:@"%@",[[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@"thumbnail_url"]];
if ([strURL length]!=0 || ![strURL isEqualToString:@""]) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strURL]];
[self setImageFromDocumentDirWithName:[[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@"thumbnail_url"] setImageToView:imgSpace imgURL:url];
return myCell;
}
}