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