Created
March 7, 2013 14:54
-
-
Save natesymer/5108568 to your computer and use it in GitHub Desktop.
Get the artwork of an mp3 audio file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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