Skip to content

Instantly share code, notes, and snippets.

@natesymer
Created March 7, 2013 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natesymer/5108568 to your computer and use it in GitHub Desktop.
Save natesymer/5108568 to your computer and use it in GitHub Desktop.
Get the artwork of an mp3 audio file.
- (void)artworksForFileAtPath:(NSString *)path block:(void(^)(NSArray *artworkImages))block {
NSURL *url = [NSURL fileURLWithPath:path];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
NSMutableArray *artworkImages = [NSMutableArray array];
for (AVMetadataItem *item in artworks) {
NSString *keySpace = item.keySpace;
UIImage *image = nil;
if ([keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *d = [item.value copyWithZone:nil];
image = [UIImage imageWithData:[d objectForKey:@"data"]];
[d release];
} else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
NSData *data = [item.value copyWithZone:nil];
image = [UIImage imageWithData:data];
[data release];
}
if (image != nil) {
[artworkImages addObject:image];
}
}
if (block) {
block(artworkImages);
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment