Parth Adroja
Parth Adroja

Reputation: 13514

Images are not correct in collection view cells

I have used sdwebimage for caching images. Now imagesArr is array of images i want to display first for all cells. I am downloading the image in background and then saving into disk so when i want those images in offline mode i can have from the disk.

But in some cases when i scroll the collectionView the images are set incorrect and then again scrolling the images appear to be correct.

This is the below code which i have implemented in cellForItemAtIndexPath. I have tried to debug but seems the data is coming correct and the image name is also correct.

Also the collectionView is laggy in scroll. Please help me to find the issue.

 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"];
}

Upvotes: 0

Views: 138

Answers (1)

isaac
isaac

Reputation: 4897

I think there are a few ways you could improve this.

It seems like you're duplicating a lot of the work that SDWebImage will do for you. That library will cache images and fetch them on demand. You should look at having that library prefetch your files into cache, rather then yourself attempting to prefetch and write them out to disk. Ultimately by the time you are in -cellForRowAtIndexPath, you only need two cases: One for setting an image when the device is offline, and then another that will either fetch or set the image from cache, which SDWebImage provides for you:

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

That's it. All the caching and switching you're doing isn't necessary. The other thing to be sure of is in your cell subclass method -prepareForReuse, you are nilling out the image.

Upvotes: 1

Related Questions