如何获取 MPMediaPlaylist 的用户自定义封面图片和描述?

How to get MPMediaPlaylist's user defined cover image and description?

我知道如何通过以下方式获取 MPMediaQuery 的标题:

MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];

NSArray *items = [playlistsQuery collections];

MPMediaPlaylist *myPlaylist = items.firstObject;

NSLog(@"%@",myPlaylist.name); //"New playlist title"

有谁知道如何访问 MPMediaPlaylist 的封面和说明?

Screenshot - User created Playlist

我还没有弄清楚自定义播放列表的插图,但我知道如何获取播放列表描述!这是 Swift 3 语法中的答案:

let allPlaylists = MPMediaQuery.playlists().collections
let playlist = allPlaylists.first

// I dunno how to get the custom playlist artwork, 
// but you can get artwork from items in the playlist:
let artwork = playlist?.representativeItem?.artwork

// The description is here:
let description = playlist?.descriptionText

获取描述很简单,只需使用

var descriptionText: String? { get }

来自 MPMediaPlaylist

要获取用户定义的图像,您必须使用私有 API,这是一个扩展,您可以使用它来获取存储在磁盘上的图像,或者获取图像的 URL:

extension MPMediaPlaylist {
    /**
     User selected image for playlist stored on disk.
     */
    var userImage: UIImage? {
        guard let catalog = value(forKey: "artworkCatalog") as? NSObject else {
            return nil
        }

        let sel = NSSelectorFromString("bestImageFromDisk")

        guard catalog.responds(to: sel),
            let value = catalog.perform(sel)?.takeUnretainedValue(),
            let image = value as? UIImage else {
            return nil
        }
        return image
    }

    /**
     URL for playlist's image.
     */
    var imageUrl: URL? {
        if let catalog = value(forKey: "artworkCatalog") as? NSObject,
            let token = catalog.value(forKey: "token") as? NSObject,
            let url = token.value(forKey: "availableArtworkToken") as? String {
            return URL(string: "https://is2-ssl.mzstatic.com/image/thumb/\(url)/260x260cc.jpg")
        }
        return nil
    }
}

一如既往,请记住,当 Apple 更改后台内容或您的应用程序可能被 App Store 拒绝时,它可能会随时停止工作。