Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 album art cover

Since iOS 8, all the data arrays for album art from iPad & iPhone devices returns a null array. I can get album art or cover art when pulling from a local file(NSBundle), but any songs on purchased from iTunes or on the device itself returns empty.

I have updated to the latest XCode, latest iOS on both devices, and the iTunes as well. I have tested on iPad 4, iPad Air, iPhone 5, iPhone 6. Hope someone knows what is going on, it seems like a known bug in iOS 8 right now. Also, I can play the asset and retrieve things like song name and artist.

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];
    }

}

All the cover art images return null or never get called. Also, pulling cover art from direct file as suggested by apple is asynchronous, but seems to take at least 10 seconds no matter what device I have tried it on. iOS 7 allowed us to directly pull the cover art from mediaItem and it was instantaneous, I do not understand why they would nerf that function.

like image 930
Tae Re Avatar asked Nov 03 '14 04:11

Tae Re


People also ask

Can you get album artwork on iPhone?

Right-click on the album cover and navigate to Album Info. In the newly opened window, go to the Artwork tab, and click Add Artwork. After you've chosen the artwork from your PC, it will be displayed in the song info menu. To make changes to your iPhone, navigate to the iPhone icon in the top left corner of the window.

How do you change the album cover on Apple music?

Select one or more songs in your music library, choose Song > Info, click Artwork, then do one of the following: Click Add Artwork, select an image file, then click Open. Drag the image file to the artwork area.


1 Answers

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];
like image 66
Henry Yu Avatar answered Oct 10 '22 03:10

Henry Yu