Get Video Thumbnail Image Asynchronously

public func getVideoThumbnailImage(url: URL) -> Image {

let urlAsset = AVURLAsset(url: url, options: nil)
let assetImageGenerator = AVAssetImageGenerator(asset: urlAsset)
assetImageGenerator.appliesPreferredTrackTransform = true
assetImageGenerator.apertureMode = .encodedPixels

let cmTime = CMTime(seconds: 1, preferredTimescale: 60)
var thumbnailCGImage: CGImage?
var errorOccurred = false

let semaphore = DispatchSemaphore(value: 0)

assetImageGenerator.generateCGImageAsynchronously(for: cmTime) { generatedImage, timeGenerated, error in
    if error == nil {
        if let cgVideoImage = generatedImage {
            thumbnailCGImage = cgVideoImage
        } else {
            errorOccurred = true
        }
    } else {
        errorOccurred = true
    }
    semaphore.signal()
    return
}

_ = semaphore.wait(timeout: .now() + 30)

if errorOccurred {
    return Image("ImageUnavailable")
}

if let thumbnailImage = thumbnailCGImage {
    let uiImage = UIImage(cgImage: thumbnailImage)
    return Image(uiImage: uiImage)
}

return Image("ImageUnavailable")

}

I am using Xcode 16 beta 2 for iOS 18 with SwiftUI.

My code above works instantly and correctly; however, Xcode produces the purple warning: "Thread running at User-interactive quality-of-service class waiting on a lower QoS thread running at Default quality-of-service class. Investigate ways to avoid priority inversions"

How can I change the code to avoid the purple warning?

@OsmanBalci This is because of a priority inversion in your code. [Diagnosing performance issues early] (https://developer.apple.com/documentation/xcode/diagnosing-performance-issues-early) covers the topic and should guide you on how to resolve resolve priority inversions.

The linked documentation is generic and not helpful unfortunately.

Get Video Thumbnail Image Asynchronously
 
 
Q