7

The UIImage "image" is always empty ("null") though the cover is shown in the music app by apple. in iOS 7 it works fine, but with iOS 8 I get no cover.

Whats wrong with my code, or what has changed in iOS 8?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"];
    MPMediaItemCollection *song = self.songsList[indexPath.row];

    cell.albumName.text = [[song representativeItem] valueForProperty: MPMediaItemPropertyAlbumTitle];

    cell.albumArtist.text = [[song representativeItem] valueForProperty:MPMediaItemPropertyAlbumArtist];


    CGSize artworkImageViewSize = CGSizeMake(100, 100);
    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *image = [artwork imageWithSize:artworkImageViewSize];

    NSLog(@"-------------------");
    NSLog(@"%@",[[song representativeItem] valueForProperty: MPMediaItemPropertyAlbumTitle]);
    NSLog(@"%@",image);

    if (artwork) {

        cell.cover.image = image;
    }
    else
    {
        cell.cover.image = [UIImage imageNamed:@"nocover.png"];
    }

    return cell;
}
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
Meins
  • 145
  • 1
  • 12

3 Answers3

11

As of iOS 8, MPMediaItem's selector imageWithSize:(CGSize)size appears to not guarantee that it will return an image. If no image is returned at the requested size, call it again with the size of the artwork bounds property:

MPMediaItemArtwork *artwork = [self valueForProperty:MPMediaItemPropertyArtwork];
UIImage *image = [artwork imageWithSize:size];
if (image == nil) {
    image = [artwork imageWithSize:artwork.bounds.size];
}
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
2

i´ve found the problem.

it´s

 CGSize artworkImageViewSize = CGSizeMake(100, 100);

the following works:

 CGSize artworkImageViewSize = CGSizeMake(120, 120);

or

 CGSize artworkImageViewSize = CGSizeMake(60, 60);

everything that can be divided by 3 works.

Meins
  • 145
  • 1
  • 12
  • I think you'll find it's multiples, starting at 60. 60, 120, 240, 360(?), 480....etc. 90 wouldn't work, and 360 wouldn't either. – topLayoutGuide Nov 16 '14 at 08:42
0

Your code looks fine. I have the same code for getting artwork and it's working for me on iOS 8. Are you SURE the item actually has artwork? Play that song in the music app - artwork?

marcshilling
  • 1,410
  • 1
  • 12
  • 19
  • yes, when I play the same song or album in the music app, the artwork there. – Meins Sep 24 '14 at 07:37
  • artwork != nil BUT image == nil is there a problem with imageWithSize: ? – Meins Sep 24 '14 at 07:51
  • I've tried it in a new project, it does not work, here the project http://www.file-upload.net/download-9572664/TestCover.zip.html – Meins Sep 24 '14 at 08:18