iosswiftxcodempmediaitem

How to Set Artwork Image for MPNowPlayingInfoCenter


I've been trying to update the artwork shown for an item in MPNowPlayingInfoCenter using MPMediaItemPropertyArtwork as shown in the following code taken from Apple's docs

if let image = UIImage(named: "image_here") {
    nowPlayingInfo[MPMediaItemPropertyArtwork] =
        MPMediaItemArtwork(boundsSize: image.size) { size in
            return image
    }
}

The issue I'm having is that this sets a smaller image as shown in the photo below in the red square. I'm trying to figure out how to set the image in the larger yellow square, and I can't find any documentation that differentiates between the two.

enter image description here

EDIT

vint's answer below clarified that the smaller icon is the app icon. I am however setting the nowPlayingInfo dictionary successfully since my other metadata is reaching the lock screen controls, like the title (blurred), duration, elapsed playback time). It's just the artwork that appears to not being working.


Solution

  • Just in case anyone finds this question and was going crazy like myself, I was able to set the image by correctly resizing my UIImage to the incoming size parameter given to the MPMediaItemArtwork callback function. As it says in the docs:

    The request handler returns an image in the newly requested size. The requested size must be less than the boundsSize parameter.

    Then setting my image to the incoming size parameter with the following extension, the MPMediaItemPropertyArtwork was finally successfully set.

    extension UIImage {
        func imageWith(newSize: CGSize) -> UIImage {
            let renderer = UIGraphicsImageRenderer(size: newSize)
            let image = renderer.image { _ in
                self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
            }
            return image.withRenderingMode(self.renderingMode)
        }
    }
    
    if let image = UIImage(named: "image_here") {
        nowPlayingInfo[MPMediaItemPropertyArtwork] =
            MPMediaItemArtwork(boundsSize: image.size) { size in
                // Extension used here to return newly sized image
                return image.imageWith(newSize: size)
        }
    }