I don't know how to make this function to return base64encoded string of thumbnail in jpeg format.
let request = QLThumbnailGenerator
.Request(fileAt: fileURL, size: size, scale: scale,
representationTypes: .lowQualityThumbnail)
QLThumbnailGenerator.shared.generateRepresentations(for: request)
{ (thumbnail, type, error) in
DispatchQueue.main.async {
if thumbnail == nil || error != nil {
// Handle the error case gracefully.
} else {
// return the thumbnail as bas64 string.
return ...
}
}
}
}
Ok I found the solution so case closed (despite I get warning from CGIMAGE 'kUTTypeJPEG' was deprecated in macOS 12.0: Use UTTypeJPEG instead.) :)
so I have method (from apple textbook)
// https://developer.apple.com/documentation/quicklookthumbnailing/creating_quick_look_thumbnails_to_preview_files_in_your_app
func giveThumbnail(for fileURL: URL, size: CGSize, scale: CGFloat) -> String{
let request = QLThumbnailGenerator
.Request(fileAt: fileURL, size: size, scale: scale,
representationTypes: .lowQualityThumbnail)
QLThumbnailGenerator.shared.generateRepresentations(for: request)
{ (thumbnail, type, error) in
DispatchQueue.main.async {
if thumbnail == nil || error != nil {
// Handle the error case gracefully.
return "ERROR"
} else {
// Display the thumbnail that you created.
let fileInBase64 = thumbnail?.cgImage.jpegData?.base64EncodedString()
return fileBase64
}
}
}
}
and extension for CGImage (found with google)
extension CGImage {
//here I Still get warning message 'kUTTypeJPEG' was deprecated in macOS 12.0: Use UTTypeJPEG instead.
var jpegData: Data? {
guard let mutableData = CFDataCreateMutable(nil, 0),
let destination = CGImageDestinationCreateWithData(mutableData, UTType.jpeg.identifier as CFString, 1, nil)
else {
return nil
}
CGImageDestinationAddImage(destination, self, nil)
guard CGImageDestinationFinalize(destination) else { return nil }
return mutableData as Data
}
}
and it works ...