David Seek
David Seek

Reputation: 17132

How to define MPMediaItemPropertyArtwork in Swift 3

This code works correct:

func setInfoCenterCredentials(_ artist: String, _ title: String, _ image: UIImage) {
    let mpic = MPNowPlayingInfoCenter.default()        
    let albumArt = MPMediaItemArtwork(image: image)
    mpic.nowPlayingInfo = [MPMediaItemPropertyTitle: title,
                           MPMediaItemPropertyArtist: artist,
                           MPMediaItemPropertyArtwork: albumArt]
}

But Xcode complains on : let albumArt = MPMediaItemArtwork(image: image),

'init(image:)' was deprecated in iOS 10.0

I have tried to just put the image as MPMediaItemPropertyArtwork: image, but that results in a crash.

How do I set it correctly for iOS 10.0? Help is very appreciated.

Edit: Yes. I have found the section in the documentation:

init(boundsSize: CGSize, requestHandler: @escaping (CGSize) -> UIImage)

But I don't know how to use it.

Upvotes: 3

Views: 3148

Answers (1)

matt
matt

Reputation: 535557

So what is this initializer asking for? It wants a size (fair enough) and it wants a function that provides the image given the size.

So you could implement it naively like this:

let albumArt = MPMediaItemArtwork(boundsSize:mySize) { sz in
    return image
}

That, with the necessary blanks filled in, should get you past the compiler (let me know if it doesn't; I haven't tested it). However, it's not a very good implementation! What you should do in the function is size your image down to the size requested in the incoming parameter (sz in my code). I think I'll leave that as an exercise for the reader...!

Upvotes: 5

Related Questions