如何从 MPMediaItemCollection 获取图稿

bun*_*ell 3 mpmediaitemcollection mpmediaquery ios mpmedialibrary swift

我的目标是获取 iPod 库中播放列表的封面图片。我做了类似的事情

 playlistMediaItemCollections = MPMediaQuery.playlistsQuery().collections ?? []
 let artworks = playlistMediaItemCollections.map { $0.valueForKey(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork }
Run Code Online (Sandbox Code Playgroud)

但它导致错误

 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MPConcreteMediaPlaylist 0x1468b1eb0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key artwork.'
Run Code Online (Sandbox Code Playgroud)

任何人都知道我如何获得播放列表艺术品?谢谢

ozg*_*gur 5

你应该使用valueForProperty

$0.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork
Run Code Online (Sandbox Code Playgroud)

但是,我认为与歌曲或专辑不同的是,MediaPlayer API 不提供这样的属性键,可以让您检索播放列表的插图。您可以查看可以与 MPMediaPlaylist 类一起使用的可能

let MPMediaPlaylistPropertyPersistentID: String
let MPMediaPlaylistPropertyName: String
let MPMediaPlaylistPropertyPlaylistAttributes: String
let MPMediaPlaylistPropertySeedItems: String
Run Code Online (Sandbox Code Playgroud)

另一种选择是,您可以在播放列表中获取歌曲的插图,然后显示其中之一或将它们组合起来为播放列表创建新的插图。

如果播放列表没有艺术品图像,我认为音乐应用程序会执行与下面相同的操作。

在此处输入图片说明

let playlist = MPMediaQuery.playlistsQuery().collections?.first
let artworks = playlist?.items.map { $0.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork }
Run Code Online (Sandbox Code Playgroud)