-1

I'm updating the album art in Control Center by doing this:

let image:UIImage = UIImage(named: title)!
let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in

            return image

        })

This works perfectly in iOS 10, but I want my app to allow both iOS 9 and 10 users. Whenever I do this, I get an error saying

init(boundsSize:requestHandler:) is only available on iOS 10 or newer

So, I need to change my code to this

let image:UIImage = UIImage(named: title)!
        if #available(iOS 10.0, *) {
            let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                return image
            })
        } else {
            // What goes here??
        }

I don't know how to do this in iOS 9. How would I do this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jacob Cavin
  • 2,169
  • 3
  • 19
  • 47
  • Are you looking for this? https://developer.apple.com/documentation/mediaplayer/mpmediaitemartwork/1621747-init –  Jul 26 '17 at 11:32

2 Answers2

1

You should use MPMediaItemArtwork(image: image) on iOS 9.

var artwork:MPMediaItemArtwork!
if let image = UIImage(named: imageName) {
    if #available(iOS 10.0, *) {
        artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
            return image
        })
    } else {
        artwork = MPMediaItemArtwork(image: image)
    }
}
//use artwork here
aytek
  • 1,842
  • 24
  • 32
0

Before iOS 10, the initializer for MPMediaArtwork, aside from simple init(), was init(image:).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok, but I need to create a variable `artwork`. I tried doing `var artwork = MPMediaItemArtwork()`, but I can't do that because `init() is unavalible` Any ideas? – Jacob Cavin Jul 26 '17 at 17:29
  • Let me get this straight. I said the initializer was `init(image:)`. You didn't try that, and you're complaining that what you _did_ try didn't work. Really? – matt Jul 26 '17 at 17:37
  • I did try that, actually. The problem is that I need a variable for `artwork`. So, my code needs to be `artwork = MPMediaItemArtwork(image: image)`. – Jacob Cavin Jul 26 '17 at 17:39
  • You would need to subclass MPMediaItemArtwork so that you can return a different image. But _that is not the question you asked_. An x-y problem. — In any case, that's not how MPMediaItemArtwork works. If you want different images, you need different MPMediaItemArtwork instances. – matt Jul 26 '17 at 17:44