iOS provides a built-in Assets Library, which stores media that you have sycned with iTunes, or stored in your Camera roll. These are usually photos or videos.
The main iOS classes for working with media Assets are the following:
- ALAssetsLibrary – which provides access to Photo Groups by category type. (ALAssetsGroupLibrary, ALAssetsGroupAlbum, ALAssetsGroupEvent, ALAssetsGroupFaces, ALAssetsGroupSavedPhotos, ALAssetsGroupAll)
- After picking an ALAssetsLibrary, you can enumerate through the enclosed ALAssetsGroup objects
- An ALAssetsGroup object holds ALAssets, which are a wrapper representing a single image or video.
Other posts cover the details on using enumerators and blocks for getting a set of ALAssets, so I will discuss retrieving metadata. This can be done in two ways:
- Call [asset metadata] which returns a dictionary containing metadata for an asset, including most of its EXIF content. When I tried this call, it did indeed work, but you may have some memory issues to deal with unless you ensure the calls are single-threaded.
- Use CGImageSourceCopyPropertiesAtIndex to retrieve individual properties from a CGImageSourceRef; this approach allows you to specify exact properties. See example code after the break below on retrieving a property from an ALAsset using this approach.
ALAssetRepresentation* representation = [[self assetAtIndex:index] defaultRepresentation]; // create a buffer to hold the data for the asset's image uint8_t *buffer = (Byte*)malloc(representation.size);// copy the data from the asset into the buffer NSUInteger length = [representation getBytes:buffer fromOffset: 0.0 length:representation.size error:nil]; if (length==0) return nil; // convert the buffer into a NSData object, free the buffer after NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:representation.size freeWhenDone:YES]; // setup a dictionary with a UTI hint. The UTI hint identifies the type of image we are dealing with (ie. a jpeg, png, or a possible RAW file) // specify the source hint NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys: (id)[representation UTI] ,kCGImageSourceTypeIdentifierHint, nil]; // create a CGImageSource with the NSData. A image source can contain x number of thumbnails and full images. CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef) adata, (CFDictionaryRef) sourceOptionsDict); [adata release]; CFDictionaryRef imagePropertiesDictionary; // get a copy of the image properties from the CGImageSourceRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL); CFNumberRef imageWidth = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelWidth); CFNumberRef imageHeight = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelHeight); int w = 0; int h = 0; CFNumberGetValue(imageWidth, kCFNumberIntType, &w); CFNumberGetValue(imageHeight, kCFNumberIntType, &h); // cleanup memory CFRelease(imagePropertiesDictionary); CFRelease(sourceRef);