MPMediaItemArtwork为空,而封面在iTunes中可用 [英] MPMediaItemArtwork is null while cover is available in iTunes

查看:407
本文介绍了MPMediaItemArtwork为空,而封面在iTunes中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImage image始终为空(null)虽然封面在Apple的音乐应用中显示。 iOS 7中的
它运行正常,但是对于iOS 8,我没有封面。

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.

我的代码有什么问题,或者iOS 8中有什么变化?

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


推荐答案

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

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

这篇关于MPMediaItemArtwork为空,而封面在iTunes中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆