How to return image from QLThumbnailGenerator

My Problem

How do I return a cgImage from QLThumbnailGenerator.shared.generateRepresentations(for: ... )?


My Hangups/Attempt (Code Below)

My attempt has a function (line 13) that returns a CGImage for a ListView entry (line 39). I tried to use an empty CGImage (line 18), but its init parameters are confusing and seem excessive for a simple thumbnail. I'm likely going about this incorrectly.


Notes


Apology/Gratitude:

Thanks for your patience, as the answer is likely obvious and my vocabulary likely off. My dumb brain tunes out lectures without a little hands-on experience, so I started this first app from zero programming knowledge as a quarantine hobby. I'll return to the theory lectures after.


Code: ResultsView.swift

import SwiftUI
import MobileCoreServices
import Combine
import QuickLookThumbnailing
import CoreImage
import UIKit
    
struct ResultsView: View {
    @EnvironmentObject var parsedScreeningData: ParsedScreeningData
    @EnvironmentObject var search: Search

    func generateThumbnail(ofThis: String) -> CGImage {
        let url = self.search.libraryFolder.appendingPathComponent(ofThis)
        let size: CGSize = CGSize(width: 68, height: 88)
        let request = QLThumbnailGenerator.Request(fileAt: url, size: size, scale: (UIScreen.main.scale), representationTypes: .all)
        let generator = QLThumbnailGenerator.shared
        
        var image = CGImage()
        
        generator.generateRepresentations(for: request) { (thumbnail, type, error) in
            DispatchQueue.main.async {
                if thumbnail == nil || error != nil {
                    assert(false, "Thumbnail failed to generate")
                } else {
                    image = thumbnail!.cgImage
                }
            }
        }
        return image
    }

    
    
var body: some View {
    VStack{

        List(search.searchResults) { datum in
            HStack {
                Image(self.generateThumbnail(ofThis: datum.PDFname), scale: (UIScreen.main.scale), label: Text("PDF"))
                Text("File: \(datum.PDFname)")
                Text("Cell line: \(self.parsedScreeningData.parsedScreeningData[datum.termFoundIndex].misidentifiedCellLine)")
                    .padding(.trailing, 10)
                    .padding(.leading, 10)
                Spacer()
                Image(systemName: "eyeglasses").foregroundColor(ColorManager.iconGreen)
                
                
//                    .onTapGesture {
//                        let url = URL.init(string: "\(datum.termFoundIndex)")
//                        guard let thisAddress = url, UIApplication.shared.canOpenURL(thisAddress) else { return }
//                        UIApplication.shared.open(thisAddress)
                } // HStack
            } // List
        }// Vstack
        .colorMultiply(ColorManager.beigeMedium)
            .padding(.trailing, 0)
            .padding(.leading, 0)
            .listStyle(GroupedListStyle())
        
    } // body
} // ResultsView struct


struct ResultsView_Previews: PreviewProvider {
    static var previews: some View {
        ResultsView().environmentObject(ParsedScreeningData()).environmentObject(Search())
    }
}

You cannot return values which are only available in a completion handler of async calls.


You may need to work with Combine framework, or need to update your `Search` to include thumbnails into its `searchResults`.

I cannot say which may be better or simpler, as your `Search` is completely hidden.

How to return image from QLThumbnailGenerator
 
 
Q