From 659df3198850d5cb87d01b26d8f1223280579c50 Mon Sep 17 00:00:00 2001 From: Jonathan Salmon Date: Mon, 7 Oct 2019 22:00:26 +0100 Subject: [PATCH] Optimised and cleaned up iOS cropping --- .../ios/Classes/document/Page.swift | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/native_pdf_renderer/ios/Classes/document/Page.swift b/packages/native_pdf_renderer/ios/Classes/document/Page.swift index 417cc223..a35bdf97 100644 --- a/packages/native_pdf_renderer/ios/Classes/document/Page.swift +++ b/packages/native_pdf_renderer/ios/Classes/document/Page.swift @@ -57,30 +57,21 @@ class Page { context!.setFillColor(backgroundColor.cgColor) context!.fill(pdfBBox) context!.drawPDFPage(renderer) - let image = UIImage(cgImage: context!.makeImage()!) + var image = UIImage(cgImage: context!.makeImage()!) + if (crop != nil){ // Perform cropping in Core Graphics let cutImageRef: CGImage = (image.cgImage?.cropping(to:crop!))! - let croppedImage: UIImage = UIImage(cgImage: cutImageRef) + image = UIImage(cgImage: cutImageRef) + } - data = croppedImage.pngData() as Data? - switch(compressFormat) { - case CompressFormat.JPEG: - data = croppedImage.jpegData(compressionQuality: 1.0) as Data? - break; - case CompressFormat.PNG: - data = croppedImage.pngData() as Data? - break; - } - } else { - switch(compressFormat) { - case CompressFormat.JPEG: - data = image.jpegData(compressionQuality: 1.0) as Data? - break; - case CompressFormat.PNG: - data = image.pngData() as Data? - break; - } + switch(compressFormat) { + case CompressFormat.JPEG: + data = image.jpegData(compressionQuality: 1.0) as Data? + break; + case CompressFormat.PNG: + data = image.pngData() as Data? + break; } success = true