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.