问题
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;
}
回答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];
});
回答2:
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.
来源:https://stackoverflow.com/questions/18032249/asynchronous-image-loading-is-not-working