为MPMediaItemArtwork声明变量(init()不可用)

时间:2017-07-26 17:47:25

标签: swift uiimage mpmediaitem

我正在为控制中心创建一个专辑封面,如下所示:

func updateNowPlayingCenter (title: String, artist: String, PlaybackRate: Double){
        var artwork = MPMediaItemArtwork()
        let image:UIImage = UIImage(named: title)!
        if #available(iOS 10.0, *) {
            artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                return image
            })
        } else {
            artwork = MPMediaItemArtwork(image: image)
        }


        var songInfo: Dictionary <NSObject, AnyObject> = [

            MPMediaItemPropertyTitle as NSObject: title as AnyObject,

            MPMediaItemPropertyArtist as NSObject: artist as AnyObject,

            MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,

            MPMediaItemPropertyPlaybackDuration as NSObject: audioPlayer.duration as AnyObject,

            MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: audioPlayer.currentTime as AnyObject,

            MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject

        ]


        MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]

    }

但是我收到了错误

  

init()不可用

artwork = MPMediaItemArtwork()行上

。那么,如何为artwork声明一个变量,以便我可以在MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,上调用它?

1 个答案:

答案 0 :(得分:0)

好的,非常简单的修复。我刚换了

if #available(iOS 10.0, *) {
            artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                return image
            })
        } else {
            artwork = MPMediaItemArtwork(image: image)
        }

let artwork = MPMediaItemArtwork(image: image)