Get album artwork from ID3 tag/Convert function from Java to Objective-C

北城余情 提交于 2019-12-03 04:02:21
- (void)loadArtworksForFileAtPath:(NSString *)path completion:(void (^)(NSArray *))completion
{
    NSURL *u = [NSURL fileURLWithPath:path];
    AVURLAsset *a = [AVURLAsset URLAssetWithURL:u options:nil];
    NSArray *k = [NSArray arrayWithObjects:@"commonMetadata", nil];

    [a loadValuesAsynchronouslyForKeys:k completionHandler: ^{
        NSArray *artworks = [AVMetadataItem metadataItemsFromArray:a.commonMetadata
             withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];

         NSMutableArray *artworkImages = [NSMutableArray array];
         for (AVMetadataItem *i in artworks)
         {
             NSString *keySpace = i.keySpace;
             UIImage *im = nil;

             if ([keySpace isEqualToString:AVMetadataKeySpaceID3])
             {
                 NSDictionary *d = [i.value copyWithZone:nil];
                 im = [UIImage imageWithData:[d objectForKey:@"data"]];
             }
             else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes])
                 im = [UIImage imageWithData:[i.value copyWithZone:nil]];

             if (im)
                 [artworkImages addObject:im];
         }

         completion(artworkImages);
     }];
}
Martin

This is just the answer of @hatfinch above but fixed and now working.

- (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;
             if ([keySpace isEqualToString:AVMetadataKeySpaceID3]) {
                 NSDictionary *d = [item.value copyWithZone:nil];
                 [artworkImages addObject:[UIImage imageWithData:[d objectForKey:@"data"]]];
             } else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
                 [artworkImages addObject:[UIImage imageWithData:[item.value copyWithZone:nil]]];
             }
         }
         if(block) {
             block(artworkImages);
         }
     }];
}

If you are merely wanting to access the artwork of the music already stored on the device i.e from the iPod application, then check out this article which demonstrates how to do it using existing frameworks provided by Apple, specifically the MediaPlayer framework.

If not, it may be wise to provide a little more information about what you are trying to achieve and also provide examples of the errors you are getting when trying to compile TagLib.

EDIT:

Another solution could be to use an embedded webview and use a JavaScript library such as this one to load, parse and fetch the album artwork?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!