0

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)
}
Ufuk Köşker
  • 1,288
  • 8
  • 29
  • The generate method is asynchronous body’s are synchronous. That should be done in an ObservableObject the function returns before the image is available. Put print statements in your function and you will see that the inner part runs after the return. – lorem ipsum Jul 14 '22 at 22:13
  • 1
    Does this answer your question https://stackoverflow.com/a/61833583/12299030? – Asperi Jul 15 '22 at 04:35

0 Answers0