Not able to get the UIImage from MPMediaItemPropertyArtWork
Asked Answered
H

5

4

I have the following code to get the UIImage from MPMediaItemPropertyArtWork to display the coverArt in my view. I get a non-NULL artwork from the mediaItem, however, when I try to extract the UIImage from the artwork, it returns NULL and I don't see anything in my UIImageView. Any help will be greatly appreciated.

CGSize artworkSize = CGSizeMake(30, 30);
UIImage *artworkImage;
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
if (artwork) {
    NSLog(@"artwork available");
    artworkImage = [artwork imageWithSize:artworkSize];
} else {
    NSLog(@"artwork not available");
    artworkImage = [UIImage imageNamed:@"EmptyAlbum.png"];
}

NSLog(@"artworkImage = %@", artworkImage);

coverArtView.image = artworkImage;
Headline answered 7/7, 2011 at 1:18 Comment(2)
Have you found any solutions yet?Interchangeable
Possible duplicate of MPMediaItemArtwork is null while cover is available in iTunesCommentary
S
3

Try with this - have use a UItableview to display album details.

declare MPMediaItemCollection *userMediaCollection;

 -(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

[self setUserMediaCollection:mediaItemCollection];

[self dismissModalViewControllerAnimated:YES];

  }


MPMediaItem *mItem = (MPMediaItem *)[userMediaCollection.items  objectAtIndex:indexPath.row];

    MPMediaItemArtwork *artWork = [mItem valueForProperty:MPMediaItemPropertyArtwork];

    cell.imageView.image = [artWork imageWithSize:CGSizeMake(30, 30)];

where userMediaCollection is your MPMediaItemCollection

Sudor answered 26/7, 2011 at 14:3 Comment(2)
Thanks Karthikeyan. But I don't see how your code is different from the one that I'd posted. It's in fact the same. The problem I see is this code works perfectly after syncing (transferring purchases) with the iTunes, but not before syncing. If I buy a song from iTunes from my iPhone and then launch my app, this code doesn't show any image. But the same works if I sync with the iTunes. Not sure what gets changed when I sync.Headline
And what I see is that the code works on my iPad but not on my iPhone.Trixi
D
1

try this... it is work.

CGSize artworkSize = CGSizeMake(30, 30);
UIImage *artworkImage;
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
artworkImage = [artwork imageWithSize:artworkSize];

if (artworkImage == nil) {
  NSLog(@"artwork not available");
  artworkImage = [UIImage imageNamed:@"EmptyAlbum.png"];
}

coverArtView.image = artworkImage;
Disini answered 20/7, 2014 at 15:40 Comment(0)
R
1

I know this question is a bit old by now, but for anyone finding this, this might help:

I was scratching my head over this for a few hours, because getting the artwork was working in one view controller in my app but not in my UITableViewController. My code looked exactly the same in both instances (and very similar to yours) except for ONE thing:

let albumArtImage = albumArt.imageWithSize(cell.imageView.bounds.size)

the SIZE! (Sorry for writing this in Swift, but you get the idea). It sounds stupid, but the MPMediaItemArtwork refused to give me a small image. I literally just changed it to a bigger size, like so:

let albumArtImage = albumArt.imageWithSize(CGSize(width: 150, height: 150))

and it worked. You can resize the UIImage to fit your needs later anyway.

If anyone finds this and this (stupid) solution works, please let me know via a reply or something.

Rajasthan answered 8/8, 2014 at 12:31 Comment(3)
This is something I have noticed as well. You should also file a radar, since this seems like an iOS 8 bug.Christianchristiana
How did you get albumArt? Is it an MPMediaItemArtwork?Scutcheon
See line 3 in the original question.Rajasthan
V
0

How do you initialize you MPMusicPlayerController? It should be:

MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
Valve answered 5/1, 2012 at 17:18 Comment(0)
C
0

See answer: https://mcmap.net/q/1439960/-mpmediaitemartwork-is-null-while-cover-is-available-in-itunes

Appears to be an iOS bug. If the image returned is nil, try using the artwork size instead.

UIImage *image = [artwork imageWithSize:size];
if (image == nil) {
    image = [artwork imageWithSize:artwork.bounds.size];
}
Commentary answered 2/3, 2016 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.