如何为 MPNowPlayingInfoCenter 设置艺术品图像 [英] How to Set Artwork Image for MPNowPlayingInfoCenter
问题描述
我一直在尝试使用 MPMediaItemPropertyArtwork
更新 MPNowPlayingInfoCenter
中某个项目显示的艺术作品,如下面的代码所示来自
编辑
vint 下面的回答澄清了较小的图标是应用程序图标.然而,我成功地设置了 nowPlayingInfo 字典,因为我的其他元数据正在到达锁定屏幕控件,如标题(模糊)、持续时间、已播放时间).这只是艺术品似乎不起作用.
以防万一有人发现这个问题并且像我一样发疯,我能够通过正确调整 UIImage
的大小来设置图像到传入 size
参数给 MPMediaItemArtwork
回调函数.正如文档中所述:
请求处理程序以新请求的大小返回图像.请求的大小必须小于 boundsSize 参数.
然后将我的 image
设置为传入的 size
参数并使用以下扩展名,MPMediaItemPropertyArtwork
终于设置成功.
扩展 UIImage {func imageWith(newSize: CGSize) ->用户界面{让渲染器 = UIGraphicsImageRenderer(size: newSize)让 image = renderer.image { _ inself.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))}返回 image.withRenderingMode(self.renderingMode)}}
I've been trying to update the artwork shown for an item in MPNowPlayingInfoCenter
using MPMediaItemPropertyArtwork
as shown in the following code taken from Apple's docs
if let image = UIImage(named: "image_here") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
The issue I'm having is that this sets a smaller image as shown in the photo below in the red square. I'm trying to figure out how to set the image in the larger yellow square, and I can't find any documentation that differentiates between the two.
EDIT
vint's answer below clarified that the smaller icon is the app icon. I am however setting the nowPlayingInfo dictionary successfully since my other metadata is reaching the lock screen controls, like the title (blurred), duration, elapsed playback time). It's just the artwork that appears to not being working.
Just in case anyone finds this question and was going crazy like myself, I was able to set the image by correctly resizing my UIImage
to the incoming size
parameter given to the MPMediaItemArtwork
callback function. As it says in the docs:
The request handler returns the an image in the newly requested size. The requested size must be less than the boundsSize parameter.
Then setting my image
to the incoming size
parameter with the following extension, the MPMediaItemPropertyArtwork
was finally successfully set.
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)
}
}
这篇关于如何为 MPNowPlayingInfoCenter 设置艺术品图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!