iOS AVFoundation: How do I fetch artwork from an mp3 file?

前端 未结 3 1203
粉色の甜心
粉色の甜心 2020-11-29 06:41

My code:

- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];

NSArray *artworks = [AVMetadataItem metadataItemsFrom         


        
3条回答
  •  抹茶落季
    2020-11-29 07:31

    As Ryan Heitner pointed out in a comment to Andy Weinstein, the way to retrieve the binary artwork data from the AVMetadataKeySpaceID3 has changed somewhere from IOS7 to IOS8. Casting blindly item to NSDictionary will crash in the ID3 case nowadays. To make code bullet proof just check dataValue, otherwise the Classes. In fact today item.value points to item.dataValue and has the NSData class.

    - (UIImage*)imageFromItem:(AVMetadataItem*)item
    {
      NSData* data=nil;
      if (item.dataValue!=nil) {
        data=item.dataValue;
      } else if ([item.value isKindOfClass:NSData.class]) { //never arrive here nowadays
        data=(NSData*)item.value;
      } else if ([item.value isKindOfClass:NSDictionary.class]) { //never arrive here nowadays...
        NSDictionary* dict=(NSDictionary*)item.value;
        data=dict[@"data"];
      }
      if (data==nil) {
        return nil;
      }
      return [UIImage imageWithData:data];
    }
    

提交回复
热议问题