I have implemented a custom gallery with two UIViewController
subclasses: AlbumController
and PhotosController
. The first one shows all gallery albums, and the second one, all photos from the selected album.
Everything works fine except when I am downloading images from a remote repository (Flickr at the moment) to the Camera Roll album. In that case, whenever I go to the AlbumController
I see all the albums, and the album cover of the Camera Roll is refreshed with the last downloaded image, but when I go to the PhotosController
for the Camera Roll album, I don't see anything. If I enter the Camera Roll album after all images have been downloaded, then I see them all.
PhotosController.h:
ALAssetsGroup *album;
PhotosController.m:
-(void) viewDidAppear:(BOOL)animated{
if( albumHasChanged ){
[self performSelectorInBackground:@selector(prepareAlbumPhotos)
withObject:nil];
[self performSelector:@selector(reloadPhotosTableView)
withObject:nil
afterDelay:.5];
albumHasChanged = NO;
}
}
#pragma mark - Private methods
-(void)prepareAlbumPhotos {
[albumAssets removeAllObjects];
@autoreleasepool {
[self.album enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
// No assets are returned while images are being downloaded!!
// ... albumAssets gets filled
}];
[self reloadPhotosTableView];
}
}
-(void) reloadPhotosTableView{
[photosTableView reloadData];
}
Inside another class I download the images and add them to the gallery like so:
UIImage *img = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
Can anybody explain why enumerateAssetsUsingBlock:
does not return any assets while writing images to its ALAssetsGroup
?
Is there a workaround to read assets from the Camera Roll album in this case?
The problem was that every time you save a new image in the phone Gallery, the reference to the Camera Roll ALAssetsGroup
changes and is no longer valid (not nil though).
In my case I was downloading and writing many images so each time I tried to view them in my gallery, the reference to the album had changed, and therefore, enumerateAssetsUsingBlock:
wasn't returning anything.
To avoid this problem, first I download all images to a folder, and then, I copy the images to the gallery blocking the application with an UIAlertView
message, with no buttons, notifying the process. With this implementation I solved the problem.
If you want to save some images at the same time, you could find another problem. Have a look at this question and answer as well: https://stackoverflow.com/a/8177014
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.