So I implemented this code to get the song artwork from Apple Music based on what song the user searched for. However, the album cover is extremely blurry even when it is merely 50x50 in size. I can't figure out what is causing this issue.
import Foundation
import SwiftUI
class ArtworkLoader {
private var dataTasks: [URLSessionDataTask] = []
func loadArtwork(forSong song: Song, completion: @escaping((Image?) -> Void)) {
guard let imageUrl = URL(string: song.artworkUrl) else {
completion(nil)
return
}
let dataTask = URLSession.shared.dataTask(with: imageUrl) { data, _, _ in
guard let data = data, let artwork = UIImage(data: data) else {
completion(nil)
return
}
let image = Image(uiImage: artwork)
completion(image)
}
dataTasks.append(dataTask)
dataTask.resume()
}
func reset() {
dataTasks.forEach { $0.cancel() }
dataTasks.removeAll()
}
}
Album cover sample after using code above: