Skip to content

Instantly share code, notes, and snippets.

@ulrikdamm
Last active December 21, 2015 04:28
Show Gist options
  • Save ulrikdamm/6249271 to your computer and use it in GitHub Desktop.
Save ulrikdamm/6249271 to your computer and use it in GitHub Desktop.
Getting artwork for a audio file in Objective-C
NSURL *url = [NSURL URLWithString:songFileLocation];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[asset loadValuesAsynchronouslyForKeys:@[ @"commonMetadata" ] completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *artwork = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
NSImage *image = nil;
for (AVMetadataItem *item in artwork) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
image = [[NSImage alloc] initWithData:[item.value copyWithZone:nil][@"data"]];
break;
} else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
image = [[NSImage alloc] initWithData:[item.value copyWithZone:nil]];
break;
}
}
// got NSImage
});
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment