I need to display document image preview. I have smth like messenger app and I want the user can see UIImage preview before opening document. Documents can be in doc, docx, docm, pptx, ppt, rtf formats.
I tried to use QuickLook library it can open that files but not generating preview UIImage from MS Office formats. I did that:
private func generatePreviewImage(for documentURL: URL?) -> UIImage? {
guard let documentURL = documentURL else { return nil }
let previewGenerator = QLThumbnailGenerator.shared
let thumbnailSize = CGSize(width: 100, height: 100)
let request = QLThumbnailGenerator.Request(fileAt: documentURL, size: thumbnailSize, scale: UIScreen.main.scale, representationTypes: .lowQualityThumbnail)
var thumbnail: UIImage?
let semaphore = DispatchSemaphore(value: 0)
previewGenerator.generateRepresentations(for: request) { thumbnailRep, _, error in
if let thumbnailImage = thumbnailRep?.uiImage {
thumbnail = thumbnailImage
}
semaphore.signal()
}
semaphore.wait()
return thumbnail
}
This works but only for not MS Office formats.