ios8 – iOS 8专辑封面

从iOS 8开始,所有来自iPad和iPad的专辑艺术数据阵列iPhone设备返回一个空数组.从本地文件(NSBundle)中提取时,我可以获得专辑封面或封面艺术,但从iTunes或设备本身购买的任何歌曲都会返回空白.

我已经更新到最新的XCode,两台设备上的最新iOS以及iTunes.我已经在iPad 4,iPad Air,iPhone 5,iPhone 6上进行了测试.希望有人知道发生了什么,它现在似乎是iOS 8中的一个已知错误.此外,我可以播放资产并检索歌曲名称和艺术家等内容.

MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
NSArray *itemsFromGenericQuery = [songQuery items];
NSMutableArray *songsList = [[NSMutableArray alloc] initWithArray:itemsFromGenericQuery];
MPMediaItem *mediaItem = (MPMediaItem *)[songsList objectAtIndex:0];

NSURL *url = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *commonArray = [assets commonMetadata];

//Test A
NSArray *albumArray = [AVMetadataItem metadataItemsFromArray:commonArray filteredByIdentifier:AVMetadataIdentifieriTunesMetadataCoverArt];
NSLog(@"commonArray = %lu",(unsigned long)[commonArray count]); //Returns 3
NSLog(@"albumArray has %lu",(unsigned long)[albumArray count]); //Returns 0 or null

//Test B
for (AVMetadataItem *metadataItem in asset.commonMetadata) {

    if ([metadataItem.commonKey isEqualToString:@"artwork"]){
        NSDictionary *imageDataDictionary = (NSDictionary *)metadataItem.value;
        NSData *imageData = [imageDataDictionary objectForKey:@"data"];
        UIImage *image = [UIImage imageWithData:imageData];
        coverArtImage.image = image;
    }
}

//Test C
for (AVMetadataItem *item in commonArray) {

    if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
        NSData *newImage = [item.value copyWithZone:nil];
        coverArtImage.image = [UIImage imageWithData:newImage];
    }

}

//Test D
for (AVMetadataItem *item in asset.metadata) {

    if ([item.commonKey isEqualToString:@"artwork"]){
        NSDictionary *imageDataDictionary = (NSDictionary *)item.value;
        NSData *imageData = [imageDataDictionary objectForKey:@"data"];
        UIImage *image = [UIImage imageWithData:imageData];
        coverArtImage.image = image;
    }

    if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
        NSData *newImage = [item.value copyWithZone:nil];
        coverArtImage.image = [UIImage imageWithData:newImage];
    }

}

所有封面艺术图像返回null或永远不会被调用.此外,苹果建议从直接文件中提取封面艺术是异步的,但无论我尝试使用哪种设备,似乎至少需要10秒钟. iOS 7允许我们直接从mediaItem中提取封面艺术并且它是瞬间的,我不明白为什么他们会对这个功能产生负面影响.

解决方法

NSString *queryString = [url query];
if (queryString == nil) // shouldn't happen
    return nil;

NSArray *components = [queryString componentsSeparatedByString:@"="];
if ([components count] < 2) // also shouldn't happen
    return nil;

id trackId = [components objectAtIndex:1];

MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:trackId forProperty:MPMediaItemPropertyPersistentID]];

NSArray *items = [query items];

[query release];

if ([items count] < 1) // still shouldn't happen
    return nil;

MPMediaItem* mpitem= [items objectAtIndex:0];

MPMediaItemArtwork* mpArt = [mpitem valueForProperty:MPMediaItemPropertyArtwork];

UIImage* mpImage = [mpArt imageWithSize:mpArt.bounds.size];

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...