如何设置 MPNowPlayingInfoCenter 的图稿图像

2024-01-04

我一直在尝试更新以下项目中显示的艺术品MPNowPlayingInfoCenter using MPMediaItemPropertyArtwork如以下代码所示,取自苹果的文档 https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_ios_and_tvos/controlling_background_audio

if let image = UIImage(named: "image_here") {
    nowPlayingInfo[MPMediaItemPropertyArtwork] =
        MPMediaItemArtwork(boundsSize: image.size) { size in
            return image
    }
}

我遇到的问题是,这设置了一个较小的图像,如下图红色方块所示。我试图弄清楚如何在较大的黄色方块中设置图像,但我找不到任何区分两者的文档。

EDIT

Vint 下面的回答澄清了较小的图标是应用程序图标。然而,我成功地设置了 nowPlayingInfo 字典,因为我的其他元数据正在到达锁定屏幕控件,例如标题(模糊)、持续时间、已播放时间)。这只是艺术品似乎不起作用。


以防万一有人发现这个问题并且像我一样疯狂,我可以通过正确调整我的大小来设置图像UIImage到传入的size给定的参数MPMediaItemArtwork回调函数。正如它在docs https://developer.apple.com/documentation/mediaplayer/mpmediaitemartwork/1649704-init:

请求处理程序返回新请求尺寸的图像。 请求的大小必须小于boundsSize 参数。

然后设置我的image到传入的size具有以下扩展名的参数,MPMediaItemPropertyArtwork终于设置成功了。

extension UIImage {
    func imageWith(newSize: CGSize) -> UIImage {
        let renderer = UIGraphicsImageRenderer(size: newSize)
        let image = renderer.image { _ in
            self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
        }
        return image.withRenderingMode(self.renderingMode)
    }
}

if let image = UIImage(named: "image_here") {
    nowPlayingInfo[MPMediaItemPropertyArtwork] =
        MPMediaItemArtwork(boundsSize: image.size) { size in
            // Extension used here to return newly sized image
            return image.imageWith(newSize: size)
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置 MPNowPlayingInfoCenter 的图稿图像 的相关文章

随机推荐