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;
Balaji Krishnan
  • 101
  • 3
  • 5
  • Have you found any solutions yet? – Bartu Mar 17 '14 at 00:50
  • Possible duplicate of [MPMediaItemArtwork is null while cover is available in iTunes](http://stackoverflow.com/questions/25998621/mpmediaitemartwork-is-null-while-cover-is-available-in-itunes) – Ric Santos Mar 02 '16 at 09:24

5 Answers5

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

Karthikeyan
  • 1,790
  • 12
  • 19
  • 1
    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. – Balaji Krishnan Jul 29 '11 at 21:16
  • And what I see is that the code works on my iPad but not on my iPhone. – mmm Oct 18 '18 at 23:41
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;
S. Ferit Arslan
  • 199
  • 3
  • 8
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.

jakeshorty
  • 11
  • 3
0

See answer: https://stackoverflow.com/a/26463261/883413

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];
}
Community
  • 1
  • 1
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
0

How do you initialize you MPMusicPlayerController? It should be:

MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
coder
  • 10,460
  • 17
  • 72
  • 125