0

Hi iam using PST Collectionview similar UIcollectionview .I want to load images from my document directory to Collection view.First tried synchronous way but it is too slow..So one know How can i load images in asynchronously to collectionview.

in my viewdidload i added following code so it will download the images to my document directory

 dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.aaa.nkp",NULL);

    dispatch_async(imageLoadQueue, ^{

        usleep(1000000);
        docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


        for(int i=0; i <[Images count] ;i++){


            imgURL = [Images objectAtIndex:i];
            [imagePreview addObject:imgURL];
            imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];


            [imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];


        }

                   [[self collectionView] reloadData];



    });

and inside collectionview cellforitem() methode

- (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    cell = nil; 
    cell = (GMMCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];
    cell.tag = indexPath.row;
    docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    BOOL isImageLoaded = YES;
     bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[Images objectAtIndex:indexPath.row]         lastPathComponent]]];

    if(bookImage == nil)
        isImageLoaded = NO;

    if(!isImageLoaded){
        [[cell grid_image] setImage:[UIImage imageNamed:@"Placeholder.png"]];

    }
    else{
       [[cell grid_image] setImage:bookImage ];

    }

    return cell;
}
Navi
  • 1,696
  • 2
  • 17
  • 36

2 Answers2

1

You need to update the UI on main thread. Try wrapping code around your [[self collectionView] reloadData]; like this:

dispatch_async(dispatch_get_main_queue(), ^{
    [[self collectionView] reloadData];
});
liuyaodong
  • 2,547
  • 18
  • 31
0

Why are you downloading the images to your directory?

To download images asynchronously, I really suggest you to use SDWebImage. It's a great lib, and can be easily installed via cocoapods.

here's the page of the project: https://github.com/rs/SDWebImage .

This lib even stores the images in cache, so I think you can remove all this download code.

After downloaded the lib and integreted to your project, just remove all the previous code, with the background download), and put this inside cellForItemAtIndexPath: of your collectionview:

 // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"anyURLhere"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Just this, like magic. The lib will put the placeHolder image in your imageView, automatically download the images asynchronously, and again automatically change the placeholder image with the downloaded one. Give a try.

If you want more information, you can check the github page of the project.

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • please tell me how can i disaply images asynchronously – Navi Aug 03 '13 at 12:38
  • not able to add that framework to my xcode ,do u have any example project using SDWebimage????? – Navi Aug 04 '13 at 07:13
  • iam getting this error "_OBJC_CLASS_$_MKAnnotationView", referenced from: – Navi Aug 04 '13 at 07:26
  • The library itself has examples : https://github.com/rs/SDWebImage/tree/master/Examples . If you are having much trouble in using as a static library, you can simply download all files here https://github.com/rs/SDWebImage/tree/master/SDWebImage and drag them inside your project in xcode. For more informations, see here https://github.com/rs/SDWebImage#installation – Lucas Eduardo Aug 04 '13 at 15:03
  • still havng same issue boss – Navi Aug 04 '13 at 18:17
  • SDWebImage can be installed with cocoapods. Easier than that, impossible. Install cocoa pods (see link: http://cocoapods.org/), put this in your created Podfile `pod 'SDWebImage', :head` and execute the command in terminal `pod isntall` – Lucas Eduardo Aug 04 '13 at 18:24