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 common@R_301_338@data];

//Test A
NSArray *albumArray = [AV@R_301_338@dataItem @R_301_338@dataItemsFromArray:commonArray filteredByIdentifier:AV@R_301_338@dataIdentifieriTunes@R_301_338@dataCoverArt];
NSLog(@"commonArray = %lu",(unsigned long)[commonArray count]); //Returns 3
NSLog(@"albumArray has %lu",(unsigned long)[albumArray count]); //Returns 0 or null

//Test B
for (AV@R_301_338@dataItem *@R_301_338@dataItem in asset.common@R_301_338@data) {

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

//Test C
for (AV@R_301_338@dataItem *item in commonArray) {

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

}

//Test D
for (AV@R_301_338@dataItem *item in asset.@R_301_338@data) {

    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:AV@R_301_338@dataKeySpaceiTunes]) {
        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];
原文链接:https://www.f2er.com/iOS/333766.html

猜你在找的iOS相关文章