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

时间:2015-12-07 11:54:11

标签: mpmediapickercontroller mpmediaquery mpmedialibrary

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

MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];

NSArray *items = [playlistsQuery collections];

MPMediaPlaylist *myPlaylist = items.firstObject;

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

有谁知道如何访问MPMediaPlaylist的封面&描述

Screenshot - User created Playlist

2 个答案:

答案 0 :(得分:0)

我还没想出自定义播放列表的作品,但我知道如何获取播放列表说明!以下是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

答案 1 :(得分:0)

获取描述很容易,只需使用

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拒绝时,它可以随时停止工作。