Hi,
I'm trying out the beta for music kit. In the current version of my app, my widget can show multiple albums. I preload the images of the album covers. In the beta's the url that is returned for the artwork starts with: "musickit://", which does not work with URLSession. How can I preload the data using the new url scheme?
Current code:
func fetchArtworkFor (musicID: MusicItemID, url : URL?) async throws -> UIImage? {
guard let url = url else {
return nil
}
let urlRequest = URLRequest (url: url)
let data = try await URLSession.shared.data(for: urlRequest)
let image = UIImage(data: data.0)
return image
}
// Some other function
for album in albumsToShow {
if let url = album.artwork?.url(width: context.family.imageHeight, height: context.family.imageHeight), let image = try? await fetchArtworkFor(musicID: album.id, url:url) {
images[album] = image
}
}
Hello @dongelen,
Thank you for your question about MusicKit's Artwork.
The best supported way to render an Artwork from MusicKit is actually to simply pass it to an ArtworkImage, which is a SwiftUI View. I highly encourage to give that a try.
That said, if you need to preload images as you described, what you're trying to do is also intended to work. Indeed, regardless of the non-standard musickit://
URL scheme, MusicKit is engineered to allow URLSession to load the artwork's image data seamlessly.
It looks like you found an edge case where that doesn't currently work as designed. In order to investigate this further, we would need you to file a ticket on Feedback Assistant, including a sysdiagnose captured right after reproducing this issue.
It would also help us to diagnose this issue if you included an NSLog
statement right before loading the data, like this:
let urlRequest = URLRequest (url: url)
NSLog("dongelen's app: about to load MusicKit artwork data for URL \(url)")
let data = try await URLSession.shared.data(for: urlRequest)
Thank you very much in advance.
Best regards,