mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-01 04:08:07 +00:00
Generate thumbnails for PDFs sent via share extension
This commit is contained in:
parent
0144957e82
commit
1293d565d0
@ -209,7 +209,7 @@ func fetchICloudFileResource(resource: ICloudFileResource) -> Signal<MediaResour
|
|||||||
if resource.thumbnail {
|
if resource.thumbnail {
|
||||||
let tempFile = TempBox.shared.tempFile(fileName: "thumb.jpg")
|
let tempFile = TempBox.shared.tempFile(fileName: "thumb.jpg")
|
||||||
var data = Data()
|
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
|
data = jpegData
|
||||||
}
|
}
|
||||||
if let _ = try? data.write(to: URL(fileURLWithPath: tempFile.path)) {
|
if let _ = try? data.write(to: URL(fileURLWithPath: tempFile.path)) {
|
||||||
|
|||||||
@ -3,7 +3,13 @@ import UIKit
|
|||||||
import Display
|
import Display
|
||||||
|
|
||||||
func generatePdfPreviewImage(url: URL, size: CGSize) -> UIImage? {
|
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 }
|
guard let firstPage = document.page(at: 1) else { return nil }
|
||||||
|
|
||||||
let context = DrawingContext(size: size)
|
let context = DrawingContext(size: size)
|
||||||
|
|||||||
@ -239,9 +239,7 @@ public class ShareRootControllerImpl {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
return EmptyDisposable
|
||||||
return ActionDisposable {
|
|
||||||
}
|
|
||||||
} |> runOn(Queue.mainQueue())
|
} |> runOn(Queue.mainQueue())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -178,7 +178,12 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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() }
|
|> mapError { _ -> Void in return Void() }
|
||||||
|> mapToSignal { event -> Signal<PreparedShareItem, Void> in
|
|> mapToSignal { event -> Signal<PreparedShareItem, Void> in
|
||||||
switch event {
|
switch event {
|
||||||
|
|||||||
@ -553,7 +553,7 @@ static void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t numBits, int32_t
|
|||||||
// add output reader to reader
|
// add output reader to reader
|
||||||
[iPodAssetReader addOutput: readerOutput];
|
[iPodAssetReader addOutput: readerOutput];
|
||||||
|
|
||||||
if (! [iPodAssetReader startReading]) {
|
if (![iPodAssetReader startReading]) {
|
||||||
NSLog(@"Unable to start reading!");
|
NSLog(@"Unable to start reading!");
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
@ -563,7 +563,6 @@ static void set_bits(uint8_t *bytes, int32_t bitOffset, int32_t numBits, int32_t
|
|||||||
int _waveformPeakCount = 0;
|
int _waveformPeakCount = 0;
|
||||||
|
|
||||||
while (iPodAssetReader.status == AVAssetReaderStatusReading) {
|
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];
|
CMSampleBufferRef nextBuffer = [readerOutput copyNextSampleBuffer];
|
||||||
|
|
||||||
if (nextBuffer) {
|
if (nextBuffer) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user