From 446e847860847acc7cfa44c6e032bc1edc0f63ed Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 26 Jun 2022 23:28:03 +0200 Subject: [PATCH] Update artwork using URLSession --- Model/Player/PlayerModel.swift | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 51462158..341b4e82 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -585,23 +585,26 @@ final class PlayerModel: ObservableObject { func updateCurrentArtwork() { guard let video = currentVideo, - let thumbnailURL = video.thumbnailURL(quality: .medium), - let thumbnailData = try? Data(contentsOf: thumbnailURL) + let thumbnailURL = video.thumbnailURL(quality: .medium) else { return } - #if os(macOS) - let image = NSImage(data: thumbnailData) - #else - let image = UIImage(data: thumbnailData) - #endif + let task = URLSession.shared.dataTask(with: thumbnailURL) { [weak self] thumbnailData, _, _ in + guard let thumbnailData = thumbnailData else { + return + } - if image.isNil { - return + #if os(macOS) + guard let image = NSImage(data: thumbnailData) else { return } + #else + guard let image = UIImage(data: thumbnailData) else { return } + #endif + + self?.currentArtwork = MPMediaItemArtwork(boundsSize: image.size) { _ in image } } - currentArtwork = MPMediaItemArtwork(boundsSize: image!.size) { _ in image! } + task.resume() } func toggleFullscreen(_ isFullScreen: Bool) {