QLThumbnailGenerator is not loading in SwiftUI

353 Views Asked by At

When I run my code with breakpoint, I see the thumbnail image, but I cannot see the image in the application. Why ?

enter image description here

enter image description here

Using:

                generateThumbnailRepresentations(url: currentFile.localPath)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 60, height: 100)
                    .aspectRatio(contentMode: .fill)

QLThumbnailGenerator:

func generateThumbnailRepresentations(url: String) -> Image {
        let pathURL = URL(fileURLWithPath: url)
        var image: UIImage = UIImage()
        // Set up the parameters of the request.
        let size: CGSize = CGSize(width: 60, height: 100)
        let scale = UIScreen.main.scale
    
    // Create the thumbnail request.
    let request = QLThumbnailGenerator.Request(fileAt: pathURL,
                                               size: size,
                                               scale: 1.0,
                                               representationTypes: .thumbnail)
    
    // Retrieve the singleton instance of the thumbnail generator and generate the thumbnails.
    let generator = QLThumbnailGenerator.shared
    generator.generateRepresentations(for: request) { (thumbnail, type, error) in
        DispatchQueue.main.async {
            if thumbnail == nil || error != nil {
                // Handle the error case gracefully.
                print("error \(error)")
            } else {
                // Display the thumbnail that you created.
                image = thumbnail?.uiImage ?? UIImage(systemName: "photo")!
                print("foo")
            }
        }
    }
    return Image(uiImage: image)
}
0

There are 0 best solutions below