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

Posted

技术标签:

【中文标题】为 MPMediaItemArtwork 声明变量(init() 不可用)【英文标题】:Declaring Variable for MPMediaItemArtwork (init() is unavailable) 【发布时间】:2017-07-26 17:47:24 【问题描述】:

我正在像这样为控制中心创建专辑封面:

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】:

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

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)

【讨论】:

以上是关于为 MPMediaItemArtwork 声明变量(init() 不可用)的主要内容,如果未能解决你的问题,请参考以下文章

MPMediaItemArtwork 和透明度

MPNowPlayingInfoCenter:从 URL 设置 MPMediaItemArtwork 的最佳方法是啥?

MPMediaItemArtwork 和 UITableView

矩形图像缩放以适应正方形以与 MPMediaItemArtwork 一起使用

使用 UICollectionView 和 MPMediaItemArtwork 遇到内存泄漏

如何启用 MPMediaItemArtwork 下一个/上一个按钮?