I often need to add large numbers of images to many different albums within the simulator for the purpose of recording screen captures of my app (which makes heavy use of images in albums). Dragging and dropping into the simulator and individually saving to albums is far too time consuming, so I'm writing an iOS app to do it automatically. Basically, it does the following:
1. Scans a specified directory for all subdirectories
2. For each subdirectory:
a. Scan for all images in the subdirectory
b. Make an album with the same name as the subdirectory (if needed)
c. Add the image to the camera roll
d. Add the new asset to the album
The problem I am having is that although all images are always added to the camera roll, not all of them get added to the album. I've put some checks in and it seems that the ALAssetsGroup is intermittently going non-editable. Here's a sample of code:
Code:
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
//search all photo albums in the library
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
//target album is found
albumWasFound = YES;
//get a hold of the photo's asset instance
[assetsLibrary assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
if (![group isEditable]) {
NSLog(@"%@ not editable", albumName);
}
[group addAsset:asset];
completionBlock(nil);
} failureBlock: completionBlock];
//album was found, bail out of the method
return;
}
// ...
That code is called from a loop which looks like this:
Code:
- (void)processImage {
//If there are no images left...
if ( ![self prImages] || ![[self prImages] count] ) {
//Tell the user we're done
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"All done" message:@"The images have been imported" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
return;
}
//Show progress
NSLog(@"Next image (%d left)", [[self prImages] count]);
//Get image data and destination album name
UIImage *image = [[[[self prImages] objectAtIndex:0] prImage] copy];
NSString *album = [[[[self prImages] objectAtIndex:0] prAlbum] copy];
NSString *path = [[[[self prImages] objectAtIndex:0] prPath] copy];
//Write the image to the album
[self saveImage:image atPath:path toAlbum:album withCompletionBlock:^(NSError *error) {
if (error!=nil) {
//Something went wrong writing the image
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
[album release];
[image release];
[path release];
}
else {
//Image should be written by now...
NSLog(@"Image done");
[album release];
[image release];
[path release];
//Remove the image from the list
[[self prImages] removeObjectAtIndex:0];
//Start processing the next image
[self processImage];
}
}];
}
In the log I can see messages like this:
Quote:
Next image (110 left)
Image done
Next image (109 left)
Image done
|
but also messages telling me that albums are not editable. The albums all have some pictures in them, so the editable state is changing during the process so some of the images don't get written. It's always different images which fail, so I don't think it's the files.
Sorry for the long post. Any ideas what I'm doing wrong?