0

I'm loading more than 30-50 images, which are stored in document directory. They all images are loaded immediately in collectionView, but when scroll collectionView at that time it will stuck.

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ImageCell";
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    //Load image from document  directory
    NSString *imageName = [arrCollection objectAtIndex:indexPath.row];
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath, imageName];
    UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:imagePath];

    cell.imgSketch.image = localImg;

    cell.txtName.text = [NSString stringWithFormat:@"%@",[imageName stringByReplacingOccurrencesOfString:@".png" withString:@""]];

    cell.layer.masksToBounds = YES;
    return cell;
}

I also tried following dispatch block

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.imgSketch.image = localImg;
                });
            });

Please suggest me any solution to overcome from this problem.

Thanks in advance

Monika Patel
  • 2,287
  • 3
  • 20
  • 45

4 Answers4

1

You can use SDWebImage it also display image from document directory.

[cell.imgSketch sd_setImageWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]]];

Note: Use fileURLWithPath when you are creating object of NSURL because its path of the file.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • How to remove image which are stored in cache? I have tried this one but it is not working `[[SDImageCache sharedImageCache] removeImageForKey:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:sender.tag]] fromDisk:YES withCompletion:nil];` – Monika Patel Aug 07 '18 at 13:47
  • @MonikaPatel Actually this should work, code looks correct, how you know that image is not deleting from cache? – Nirav D Aug 07 '18 at 13:50
  • Actually my application is work only in local, now user can import multiple images from gallery to my application. All images name is in this sequence img1,img2,im3....n. Now the point is if user select 3 images and import all 3 imgs in my application. So now image name is assign img1,img2,im3. Now user delete this 3 images and import new 3 images. Again image name will img1,img2,img3. So these img1,img2,img3 image get old image from cache. and it will show old deleted image insted of new 3 selected images. – Monika Patel Aug 07 '18 at 13:56
  • @MonikaPatel Can you please try once clearing the cache for all image please check this answer for more details https://stackoverflow.com/a/39881690/6433023 – Nirav D Aug 07 '18 at 14:02
0

I suggest to load all the images only once out of cellForRowAt

NSMutableArray*images; // every instance is of type UIImage not imageName

instead of this

UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];

Note: this method initWithContentsOfFile runs in mainQueue so it's the main reason of the stuck

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

What I would suggest is to move your image loading operation to background thread.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
   UIImage* localImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];    
   dispatch_async(dispatch_get_main_queue(), ^{
      cell.imgSketch.image = localImage;
   });
});
bseh
  • 447
  • 7
  • 13
0

The collectionview/tableview cells will have flickering if we use large images. Try to use low size image or create one at cellForItemAtIndexPath in background queue.

You can improve performance by caching image in an array. Note: Try to clean it if the capacity increases a limit.

cell.imgSketch.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

     UIImage *localImg = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", (AppObj).imagefolderPath, [arrCollection objectAtIndex:indexPath.row]]];

     NSData *imgData = UIImageJPEGRepresentation(localImg, 0.5);
     localImg = [UIImage imageWithData:imgData];

     dispatch_async(dispatch_get_main_queue(), ^{
         ImageCell *imageCell = [collectionView cellForItemAtIndexPath:indexPath];
         if (imageCell) {
             cell.imgSketch.image = localImg;
         }
    });
});
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84