I am trying to get the artwork for the now playing song in the iOS Music app. The artwork is displayed correctly in the iOS Music app however calling MPMediaItemArtwork.bounds.size
returns (0,0)
meaning MPMediaItemArtwork.image(at: CGSize)
fails to return an image. It sounds similar to this issue from iOS 8 but implementing the suggestions there didn't fix my issue.
The issue seems to be related to items that are in Apple Music but not added to the user's library however some songs that aren't in the user's library do load artwork. Sometimes force closing the iOS Music app fixes the issue but other times not.
This is the code I'm using to get the image.
mediaItem = MPMusicPlayerController.systemMusicPlayer.nowPlayingItem
if let artwork = mediaItem.artwork {
// Forcing a size using artwork.image(at: CGSize(width: 120, height: 120)) also fails
if let image = artwork.image(at: artwork.bounds.size) {
artworkImage = image
return
}
}
I can occasionally see an error in the Xcode console like below but the message doesn't happen every time the artwork fails to load and I can't reproduce it with any consistency (i.e. loading artwork for the same song doesn't always produce the error).
2020-06-03 11:03:11.763148+1000 App[] [Artwork] [MPArtwork]
HALTING -- Error loading artwork for catalog:
<MPArtworkCatalog: 0x2801b8c40 token=<MPMusicPlayerControllerNowPlaying: 0x282403120> fittingSize={0, 0} destinationScale=3.000000 destination=(null)>
Error Domain=MPMusicPlayerControllerErrorDomain Code=7 "b1..5A:https://is3-ssl.mzstatic.com/image/thumb/Music5/v4/82/ce/dc/82cedc46-4097-a400-a496-b438faecfc49/dj.eaozurbq.jpg/1425x1425bb.jpg does not support artwork."
UserInfo={NSDebugDescription=b1...5A:https://is3-ssl.mzstatic.com/image/thumb/Music5/v4/82/ce/dc/82cedc46-4097-a400-a496-b438faecfc49/dj.eaozurbq.jpg/1425x1425bb.jpg does not support artwork.}