iOS - MPMediaItem 显示默认图稿

发布于 2024-12-11 05:59:02 字数 625 浏览 1 评论 0原文

我目前正在开发一个应用程序,可以显示您正在音乐播放器中收听的艺术家、曲目和专辑封面。一切都很顺利,除了当我播放一首没有艺术作品的歌曲时,我希望能够显示我自己的默认图像(而不是显示空白屏幕)。

下面是我想象它应该如何工作,但它永远不会进入其他,因为 itemArtwork 永远不会为零!

感谢您的帮助。

谢谢,本

_item = [_player nowPlayingItem];
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];

if (itemArtwork != nil) {
    UIImage *albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
    _albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
    NSLog(@"No ALBUM ARTWORK");
    _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}

I am currently developing an app that shows what artist, track and album art you're listening to in the Music player. All is going well apart from when I play a song with no artwork I want to be able to show my own default image (as opposed to showing a blank screen).

The below is how I imagined it SHOULD work however it never gets into the else as the itemArtwork is never nil!

You're help is appreciated.

Thanks, Ben

_item = [_player nowPlayingItem];
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];

if (itemArtwork != nil) {
    UIImage *albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
    _albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
    NSLog(@"No ALBUM ARTWORK");
    _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-12-18 05:59:03

MPMediaItemArtwork 似乎始终存在,即使对于没有插图的曲目也是如此。

我检测是否没有图像的方法是查看 MPMediaItemArtwork 的 imageWithSize 是否返回 NULL。

或者,稍微调整一下你的代码:

_item = [_player nowPlayingItem];
UIImage *albumArtworkImage = NULL;
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];

if (itemArtwork != nil) {
    albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
}

if (albumArtworkImage) {
    _albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
    NSLog(@"No ALBUM ARTWORK");
    _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}

我希望这些信息可以帮助你(如果是这样,请将此答案标记为选中:-)

MPMediaItemArtwork seem to always exist, even for tracks that don't have artwork.

The way I detect if there's no image is to see if MPMediaItemArtwork's imageWithSize returns NULL.

Or, rejiggering your code a bit:

_item = [_player nowPlayingItem];
UIImage *albumArtworkImage = NULL;
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];

if (itemArtwork != nil) {
    albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
}

if (albumArtworkImage) {
    _albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
    NSLog(@"No ALBUM ARTWORK");
    _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}

I hope this info helps you out (and if so, mark this answer as checked :-)

厌倦 2024-12-18 05:59:03

如果您只需要检查艺术品是否存在(而不可能抓取图像,这会消耗大量 CPU 周期),您还可以检查 itemArtwork.bounds 属性。如果艺术品不存在,则 bounds.size.widthbounds.size.height 属性将为 0:

MPMediaItemArtwork *artwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
BOOL hasArtwork = (artwork.bounds.size.width > 0 && artwork.bounds.size.height > 0);

If you just need to check if the artwork exists or not (without possibly grabbing the image, which burns a lot of CPU cycles) you can also check the itemArtwork.bounds property. If the artwork does not exist, the bounds.size.width and bounds.size.height properties will be 0:

MPMediaItemArtwork *artwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
BOOL hasArtwork = (artwork.bounds.size.width > 0 && artwork.bounds.size.height > 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文