Accessing all your onboard photos from your iPhone database
Last weekend, I decided to expand Coverflow to work with my entire photo album. This involved exploring the MusicLibrary and PhotoLibrary frameworks to find out how I could extract a complete set of albums and their pictures. The PLPhotoLibrary class proved to hold the key. With it, I could request an album list, and then build up a dictionary of photos that linked back from the image identifier to the album it came from.
id pl = [[PLPhotoLibrary alloc]
initWithPath:@"/var/mobile/Media/Photos"];
picList = [[NSMutableDictionary alloc] init];
int num = [[pl albums] count];
// Add every photo in the library
int i, j;
for (i = 0; i < num; i++)
{
album = [[pl albums] objectAtIndex:i];
id imgs = [album images];
for (j = 0; j < [imgs count]; j++) {
id imgid = [NSNumber numberWithInt:
[[imgs objectAtIndex:j] imageID]];
[picList setObject:album forKey:imgid];
}
}
Once the photos were all gathered, I could then pull back out each photo's album, and request a low-quality (thus memory-saving) CGImageRef.
// Image Data Source Method
- (void) coverFlow:(id)cf requestImageAtIndex: (int)idx quality: (int)q
{
id imgID = [[picList allKeys] objectAtIndex:idx];
id album = [picList objectForKey:imgID];
id img = [[album imageWithImageID:[imgID integerValue]]
createLowResolutionFullScreenCGImageRef];
[cfLayer setImage:img atIndex:idx type:q];
}
Together this allowed me to update my Coverflow sample to work with all onboard images. As you'll find when running this sample, I have not yet figured out some sort of matting scheme, so all the images are squashed (regardless of original aspect ratio) into a square.
The sample code is available as FullAlbum.m. You can also play with PhotoPhun, a simple non-coverflow application built over this library code.
Categories
MacComments (1)
Read More Entries by Erica Sadun.

did you remember to release your picList? :) just checking.