【问题标题】:URL to show in MPMediaItemPropertyArtwork在 MPMediaItemPropertyArtwork 中显示的 URL
【发布时间】:2019-11-25 14:54:01
【问题描述】:

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

我知道我首先需要对其进行友好的 URL 编码 -

.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

例如

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

但是,我如何将其放入 MPMediaItemPropertyArtwork。

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

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

http://covers.drn1.com.au/az_B101753_Wish You Were Here_Brain Purist.jpg

【问题讨论】:

    标签: ios swift mpmediaitem


    【解决方案1】:

    您需要设置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)
            }
        }
    

    【讨论】:

    • 您不能让同步方法等待异步方法返回其值
    • func getArtBoard(from url: URL, completion: @escaping (MPMediaItemArtwork) -> () ) { URLSession.shared.dataTask(with: url) { data, _, error in guard let data = data, let image = UIImage(data: data) else { if let error = error { print(error) } return } completion(.init(boundsSize: image.size) { _ in image }) }.resume() }
    • 然后getArtBoard(from: url) { artboard in // do whatever with your artboard print("finished downloading artboard", artboard.bounds) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多