Generate thumbnails for PDFs sent via share extension

This commit is contained in:
Ilya Laktyushin 2019-07-15 18:13:44 +02:00
parent 0144957e82
commit 1293d565d0
5 changed files with 42 additions and 34 deletions

View File

@ -209,7 +209,7 @@ func fetchICloudFileResource(resource: ICloudFileResource) -> Signal<MediaResour
if resource.thumbnail {
let tempFile = TempBox.shared.tempFile(fileName: "thumb.jpg")
var data = Data()
if let image = generatePdfPreviewImage(url: url, size: CGSize(width: 320.0, height: 320.0)), let jpegData = UIImageJPEGRepresentation(image, 0.5) {
if let image = generatePdfPreviewImage(url: url, size: CGSize(width: 256, height: 256.0)), let jpegData = UIImageJPEGRepresentation(image, 0.5) {
data = jpegData
}
if let _ = try? data.write(to: URL(fileURLWithPath: tempFile.path)) {

View File

@ -3,7 +3,13 @@ import UIKit
import Display
func generatePdfPreviewImage(url: URL, size: CGSize) -> UIImage? {
guard let document = CGPDFDocument(url as CFURL) else { return nil }
guard let data = try? Data(contentsOf: url, options: .mappedIfSafe) else { return nil }
return generatePdfPreviewImage(data: data, size: size)
}
func generatePdfPreviewImage(data: Data, size: CGSize) -> UIImage? {
guard let provider = CGDataProvider(data: data as CFData) else { return nil }
guard let document = CGPDFDocument(provider) else { return nil }
guard let firstPage = document.page(at: 1) else { return nil }
let context = DrawingContext(size: size)

View File

@ -239,9 +239,7 @@ public class ShareRootControllerImpl {
}
break
}
return ActionDisposable {
}
return EmptyDisposable
} |> runOn(Queue.mainQueue())
}

View File

@ -178,7 +178,12 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
}
}
} else {
return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(data), mimeType: mimeType, attributes: [.FileName(fileName: fileName ?? "file")], hintFileIsLarge: data.count > 5 * 1024 * 1024)
var thumbnailData: Data?
if mimeType == "application/pdf", let image = generatePdfPreviewImage(data: data, size: CGSize(width: 256.0, height: 256.0)), let jpegData = UIImageJPEGRepresentation(image, 0.5) {
thumbnailData = jpegData
}
return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(data), thumbnailData: thumbnailData, mimeType: mimeType, attributes: [.FileName(fileName: fileName ?? "file")], hintFileIsLarge: data.count > 5 * 1024 * 1024)
|> mapError { _ -> Void in return Void() }
|> mapToSignal { event -> Signal<PreparedShareItem, Void> in
switch event {

View File

@ -553,7 +553,7 @@ static void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t numBits, int32_t
// add output reader to reader
[iPodAssetReader addOutput: readerOutput];
if (! [iPodAssetReader startReading]) {
if (![iPodAssetReader startReading]) {
NSLog(@"Unable to start reading!");
return nil;
}
@ -563,7 +563,6 @@ static void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t numBits, int32_t
int _waveformPeakCount = 0;
while (iPodAssetReader.status == AVAssetReaderStatusReading) {
// Check if the available buffer space is enough to hold at least one cycle of the sample data
CMSampleBufferRef nextBuffer = [readerOutput copyNextSampleBuffer];
if (nextBuffer) {