7

尽管封面显示在苹果的UIImage音乐应用程序中,但“图像”始终为空(“null”)。在 iOS 7 中它工作得很好,但在 iOS 8 中我没有掩饰。

我的代码有什么问题,或者 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;
}
4

3 回答 3

11

从 iOS 8 开始,MPMediaItem的选择器imageWithSize:(CGSize)size似乎不能保证它会返回图像。如果没有以请求的大小返回图像,请使用艺术品bounds属性的大小再次调用它:

MPMediaItemArtwork *artwork = [self valueForProperty:MPMediaItemPropertyArtwork];
UIImage *image = [artwork imageWithSize:size];
if (image == nil) {
    image = [artwork imageWithSize:artwork.bounds.size];
}
于 2014-10-20T10:16:24.393 回答
2

我发现了问题。

它的

 CGSize artworkImageViewSize = CGSizeMake(100, 100);

以下作品:

 CGSize artworkImageViewSize = CGSizeMake(120, 120);

或者

 CGSize artworkImageViewSize = CGSizeMake(60, 60);

一切可以被 3 作品分割的东西。

于 2014-09-24T12:36:36.723 回答
0

你的代码看起来不错。我有相同的获取艺术品的代码,它在 iOS 8 上为我工作。你确定该项目实际上有艺术品吗?在音乐应用程序中播放那首歌 - 艺术品?

于 2014-09-23T17:55:26.510 回答