ios8 – iOS 8专辑封面

前端之家收集整理的这篇文章主要介绍了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相关文章