Rus*_*wer 4 mpmediaitem ios swift
所以我想显示来自 URL 的图像。
我知道我首先需要对其进行友好的 URL 编码 -
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
Run Code Online (Sandbox Code Playgroud)
例如
if var strUrl = nowplaying.data.first?.track.imageurl {
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
Run Code Online (Sandbox Code Playgroud)
但是,我如何才能将其放入 MPMediaItemPropertyArtwork 中。
我尝试了许多使用第三部分翠鸟插件的不同方法 - 但没有成功。
如果有人可以告诉我如何让下图显示那就太好了
http://covers.drn1.com.au/az_B101753_Wish You Were Here_Brain Purist.jpg
您需要MPRemoteCommandCenter使用它进行设置nowPlayingInfo:
func setupNowPlayingInfo(with artwork: MPMediaItemArtwork) {
nowPlayingInfo = [
MPMediaItemPropertyTitle: "Some name",
MPMediaItemPropertyArtist: "Some name",
MPMediaItemPropertyArtwork: artwork,
MPMediaItemPropertyPlaybackDuration: CMTimeGetSeconds(currentItem.duration)
MPNowPlayingInfoPropertyPlaybackRate: 1,
MPNowPlayingInfoPropertyElapsedPlaybackTime: CMTimeGetSeconds(currentItem.currentTime())
]
}
Run Code Online (Sandbox Code Playgroud)
func getData(from url: URL, completion: @escaping (UIImage?) -> Void) {
URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
if let data = data {
completion(UIImage(data:data))
}
})
.resume()
}
func getArtBoard() {
guard let url = URL(string: "http://covers.drn1.com.au/az_B101753_Wish%20You%20Were%20Here_Brain%20Purist.jpg") else { return }
getData(from: url) { [weak self] image in
guard let self = self,
let downloadedImage = image else {
return
}
let artwork = MPMediaItemArtwork.init(boundsSize: downloadedImage.size, requestHandler: { _ -> UIImage in
return downloadedImage
})
self.setupNowPlayingInfo(with: artwork)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |