我目前正在使用以下代码打印出现在播放的图像。
if var strUrl = nowplaying.data.first?.track.imageurl {
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
self.imageurl.kf.setImage(with: URL(string: strUrl), placeholder: nil)
}
我想将该图像发送至
MPMediaItemPropertyArtwork
位于以下函数中
func nowplaying(artist: String, song: String, artwork: String){
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle:song,
MPMediaItemPropertyArtist:artist,
MPMediaItemPropertyArtwork:artwork
]
}
目前我有 MusicPlayer.shared.nowplaying(artist: $0.track.artist, Song: $0.track.title, arts: $0.track.imageurl)
正确发送数据,但随后崩溃 - 我知道这是因为您无法在 MPMediaItemProperty
的图稿部分中拥有远程图像...
所以我想知道如何缓存图像并发送它?
已尝试代码
let artwork = MPMediaItemArtwork(boundsSize: CGSize(width: 100, height: 100)) { size -> UIImage in
KingfisherManager.shared.retrieveImage(with: cover, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
print(image)
})
return UIImage()
}
它打印在控制台日志中
Optional(<UIImage:0x280a4e0a0 anonymous {500, 500}>)
但似乎没有将其打印到
MPMediaItemPropertyArtwork
最佳答案
您可以将图像保存在缓存中,并可以在需要时检索它。
let resource = ImageResource(downloadURL: url, cacheKey: "my_cache_key")
imageView.kf.setImage(with: resource)
您可以使用
从缓存中检索图像cache.retrieveImage(forKey: "cacheKey") { result in
//Your code
)}
欲了解更多信息,您可以查看此链接https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet
希望这有效...
关于ios - swift Kingfisher 和 MPMediaItemArtwork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59032267/