Navi
Navi

Reputation: 1686

How to Display first 9 images in UICollectionview?

I have a image url plist with 50 image urls.Iam trying to display that in a UICollectionview but it getting too slow.Iam displaying 9 cells(images) at a time like 3x3 (3 rows and 3 columns).How can i display first 9 images at loading time and next 9 images on next scrolling .?is that possible? i tried SDWebImages for loading images ,Its not working for me.Please help me.

CODE INSIDE collectionView cellForItemAtIndexPath: () the code inside comment take lots of time for loading image,that code is used for loading image from document directory if image is not there then i will display in one place holder image

    cell = nil;

    cell = (GMMCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];

    cell.tag = indexPath.row;
     docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];



    NSString *path;
    BOOL isImageLoaded = YES;

/////////////////////PROBLEM CODE:TAKING TOO MUCH TIME TO LOAD IMAGE //////////////////////

  bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[allbooksImage objectAtIndex:indexPath.row] lastPathComponent]]];

//////////////////////////////////////////////////////////////////////////////////

    if(bookImage == nil){
        isImageLoaded = NO;
    }
    if(!isImageLoaded){
        [[cell grid_image] setImage:[UIImage imageNamed:@"App-icon-144x144.png"]];
   } else{

       [[cell grid_image] setImage:bookImage];





    }

    sharedManager.bookID=bookId;

return cell;

This is what iam expecting

enter image description here

But iam getting like this , App Loading time Without any scroll (first image:3 columns are empty) enter image description here

App screen after first scroll (second image:last column displaying 2 images ,rest of the part still empty ) enter image description here

App screen after a long scroll (third image: last colum some images are there ,rest of the columns still empty) enter image description here

App screen again scroll to top,so the first image changed enter image description here

Upvotes: 3

Views: 1492

Answers (1)

Exploring
Exploring

Reputation: 935

You can try the below code

[[cell grid_image] setImage:nil];

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(concurrentQueue, ^{
    bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[allbooksImage objectAtIndex:indexPath.row] lastPathComponent]]];
    dispatch_async(dispatch_get_main_queue(), ^{
        if (bookImage)
        {
            [[cell grid_image] setImage:bookImage];
        }
        else
        {
            [[cell grid_image] setImage:[UIImage imageNamed:@"App-icon-144x144.png"]];
        }
    });

});

hope it will work for you.

Upvotes: 1

Related Questions