ios - 在 MPMediaItemPropertyArtwork 中显示的 URL

标签 ios swift mpmediaitem

所以我想显示来自 URL 的图像。

我知道我首先需要将其编码为友好的 URL -

.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

例如

if var strUrl = nowplaying.data.first?.track.imageurl {
  strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
 }

但是我如何才能将它放入 MPMediaItemPropertyArtwork。

我尝试了很多与使用第 3 部分 kingfisher 插件不同的事情 - 但没有成功。

如果有人能告诉我如何得到下面的图像来显示那会很棒

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())
        ]
    }
   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)
        }
    }

enter image description here

关于ios - 在 MPMediaItemPropertyArtwork 中显示的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59034500/

相关文章:

ios - 如何在修改路径时重绘 CAShapeLayer?

swift - 从一个 UTM 区域到另一区域的项目点

ios - UINavigation 设置后退按钮图像而不是 <Back text Swift 2.0

arrays - 没有得到数组的最大元素

objective-c - 从 iOS 库中选择音乐并发送/保存

ios - 在 iOS 中停止 View 旋转

ios - 将图像下载到设备 IOS7 中的本地存储

ios - UITableViewCell setSelected :animated called twice on iPad

iphone - 艺术家专辑数量

iphone - 同步后立即从 iPod 库加载歌曲