0
votes

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;
    }
}
2
Do you have the source code where you set the cell content? I had a problem similar to you. I'm not sure if this is your case, but when you scroll a tableview or a collectionview, iOS try to reuse cells with the same type. So If you have loaded a previous image in an existing cell, the new image you are downloading to show in a "new" cell, is not going to be showed until you have downloaded it. So, the old one is going to be until that. - EnriMR
UIImageView *imgSpace = (UIImageView *)[myCell viewWithTag:100]; [self setImageFromDocumentDirWithName:[[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@"thumbnail_url"] setImageToView:imgSpace imgURL:url]; - Nilesh Mahajan
I am passing imgview on which I want to show this image as a parameter to setImageFromDocumentDirWithName method. - Nilesh Mahajan
Do you have a custom UICollectionViewCell? Because it will be easier. You could do inside cellForItemAtIndexPath-> gist.github.com/enrimr/eb69f90ae2f7e69f999e (It is better to share source code through gist :) ) - EnriMR
the problem with your code is that it's not setting the image of the imageView on the cell of the intended index path, for more info see the below answers. - Sanjay Mohnani

2 Answers

0
votes

It's the issue which is related to reusing the cell & also setting the image to the incorrect cell.

In UICollectionView or UITableView, to achieve maximum performance and reducing memory consumption the concept of reusable cells is used. The basic concept is that the collection view and table view initially create only visible cells and as we scroll the already created cells are reused with a requirement to update the subviews(containers) of the cells with the content of the respective index path(cell).

In your case when you create a collection view the images for the cells will be shown up correctly until you scroll. But as soon as you scroll the cells are reused and images intended to be displayed on a cell does not get displayed there.

To overcome this issue you need to update the cell's image based on data for the index path to the cell of the same index path.

Also if you need to know the exact problem in code than please post the source of you cell creation and setting the image to the cell.

0
votes

Can you do one thing, just add one more parameter "IndexPath" in the method -

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url

as

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url forIndexPath:(NSIndexPath *)indexPath

and replace it's definition with -

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url forIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

if(cell ! =nil)
{
     UIImageView *imageView = [cell viewWithTag:100];

     NSString *workSpacePath=[[Utility applicationDocumentsDirectory] stringByAppendingPathComponent:strImageName];

    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:workSpacePath];
    if (fileExists) {
        imageView.image =[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
    }
    else{
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default1"]];
    }
}
}