Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

ios - MPMediaItemArtwork is null while cover is available in iTunes

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...