diff --git a/ROThumbnailGenerator.podspec b/ROThumbnailGenerator.podspec index a54aed1..6148621 100644 --- a/ROThumbnailGenerator.podspec +++ b/ROThumbnailGenerator.podspec @@ -11,12 +11,12 @@ Pod::Spec.new do |spec| spec.name = 'ROThumbnailGenerator' - spec.version = '2.1.1' + spec.version = '2.1.2' spec.license = { :type => 'MIT' } spec.homepage = 'https://github.com/prine/ROThumbnailGenerator' spec.authors = { 'Robin Oster' => 'prine.dev@gmail.com' } spec.summary = 'Creates thumbnails of different file types very easily' - spec.source = { :git => 'https://github.com/prine/ROThumbnailGenerator.git', :tag => "2.1.1" } + spec.source = { :git => 'https://github.com/prine/ROThumbnailGenerator.git', :tag => "2.1.2" } spec.source_files = 'Source/**/*' spec.framework = 'SystemConfiguration' spec.ios.deployment_target = '8.4' diff --git a/ROThumbnailGenerator.xcodeproj/project.pbxproj b/ROThumbnailGenerator.xcodeproj/project.pbxproj index 7b2493e..c43a2a8 100644 --- a/ROThumbnailGenerator.xcodeproj/project.pbxproj +++ b/ROThumbnailGenerator.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ CB3438B41B7B6A8900EEB5DB /* TestPdf.pdf in Resources */ = {isa = PBXBuildFile; fileRef = CB3438B31B7B6A8900EEB5DB /* TestPdf.pdf */; }; CB3438BC1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec in Resources */ = {isa = PBXBuildFile; fileRef = CB3438BA1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec */; }; CB3438BE1B7B790800EEB5DB /* ROThumbnail.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB3438BD1B7B790800EEB5DB /* ROThumbnail.swift */; }; + CB48C11A1DAD440800B7CD15 /* TestPdf2.pdf in Resources */ = {isa = PBXBuildFile; fileRef = CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -57,6 +58,7 @@ CB3438B91B7B71BB00EEB5DB /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; }; CB3438BA1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ROThumbnailGenerator.podspec; sourceTree = SOURCE_ROOT; }; CB3438BD1B7B790800EEB5DB /* ROThumbnail.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ROThumbnail.swift; path = Source/ROThumbnail.swift; sourceTree = SOURCE_ROOT; }; + CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = TestPdf2.pdf; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -117,6 +119,7 @@ CB3438AF1B7B6A3C00EEB5DB /* TestMovie.mov */, CB3438B11B7B6A4900EEB5DB /* TestImage.png */, CB3438B31B7B6A8900EEB5DB /* TestPdf.pdf */, + CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */, CB3438791B7B3BAF00EEB5DB /* Info.plist */, ); name = "Supporting Files"; @@ -238,6 +241,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + CB48C11A1DAD440800B7CD15 /* TestPdf2.pdf in Resources */, CB3438B21B7B6A4900EEB5DB /* TestImage.png in Resources */, CB3438801B7B3BAF00EEB5DB /* Main.storyboard in Resources */, CB3438B01B7B6A3C00EEB5DB /* TestMovie.mov in Resources */, diff --git a/ROThumbnailGenerator/Info.plist b/ROThumbnailGenerator/Info.plist index 99ed637..0c05aaa 100644 --- a/ROThumbnailGenerator/Info.plist +++ b/ROThumbnailGenerator/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.0.1 + 2.1.2 CFBundleSignature ???? CFBundleVersion - 15265 + 16295 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/ROThumbnailGenerator/TestPdf2.pdf b/ROThumbnailGenerator/TestPdf2.pdf new file mode 100644 index 0000000..bbc0092 Binary files /dev/null and b/ROThumbnailGenerator/TestPdf2.pdf differ diff --git a/ROThumbnailGenerator/ViewController.swift b/ROThumbnailGenerator/ViewController.swift index e69ed95..9e8d73b 100644 --- a/ROThumbnailGenerator/ViewController.swift +++ b/ROThumbnailGenerator/ViewController.swift @@ -22,7 +22,8 @@ class ViewController: UIViewController { let imageURL = Bundle.main.url(forResource: "TestImage", withExtension: "png") testImage.image = thumbnail.getThumbnail(imageURL!) - let documentURL = Bundle.main.url(forResource: "TestPdf", withExtension: "pdf") + // let documentURL = Bundle.main.url(forResource: "TestPdf", withExtension: "pdf") + let documentURL = Bundle.main.url(forResource: "TestPdf2", withExtension: "pdf") testPdf.image = thumbnail.getThumbnail(documentURL!) let movieURL = Bundle.main.url(forResource: "TestMovie", withExtension: "mov") diff --git a/Source/PDFThumbnailGenerator.swift b/Source/PDFThumbnailGenerator.swift index 0f8b95b..c30e120 100644 --- a/Source/PDFThumbnailGenerator.swift +++ b/Source/PDFThumbnailGenerator.swift @@ -13,38 +13,42 @@ class PDFThumbnailGenerator : ROThumbnailGenerator { var supportedExtensions:Array = ["pdf"] func getThumbnail(_ url:URL, pageNumber:Int, width:CGFloat) -> UIImage { - let pdf:CGPDFDocument = CGPDFDocument(url as CFURL)!; - - let firstPage = pdf.page(at: pageNumber) - - var pageRect:CGRect = firstPage!.getBoxRect(CGPDFBox.mediaBox); - let pdfScale:CGFloat = width/pageRect.size.width; - pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale); - pageRect.origin = CGPoint.zero; - - UIGraphicsBeginImageContext(pageRect.size); - - let context:CGContext = UIGraphicsGetCurrentContext()!; - - // White BG - context.setFillColor(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0); - context.fill(pageRect); - - context.saveGState(); - - // Next 3 lines makes the rotations so that the page look in the right direction - context.translateBy(x: 0.0, y: pageRect.size.height); - context.scaleBy(x: 1.0, y: -1.0); - context.concatenate((firstPage?.getDrawingTransform(CGPDFBox.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))!); - - context.drawPDFPage(firstPage!); - context.restoreGState(); - - let thm:UIImage = UIGraphicsGetImageFromCurrentImageContext()!; - - UIGraphicsEndImageContext(); - return thm; - + if let pdf:CGPDFDocument = CGPDFDocument(url as CFURL) { + + if let firstPage = pdf.page(at: pageNumber) { + + var pageRect:CGRect = firstPage.getBoxRect(CGPDFBox.mediaBox) + let pdfScale:CGFloat = width/pageRect.size.width + pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale) + pageRect.origin = CGPoint.zero + + UIGraphicsBeginImageContext(pageRect.size) + + if let context:CGContext = UIGraphicsGetCurrentContext() { + + // White BG + context.setFillColor(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0) + context.fill(pageRect) + + context.saveGState() + + // Next 3 lines makes the rotations so that the page look in the right direction + context.translateBy(x: 0.0, y: pageRect.size.height) + context.scaleBy(x: 1.0, y: -1.0) + context.concatenate((firstPage.getDrawingTransform(CGPDFBox.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))) + + context.drawPDFPage(firstPage) + context.restoreGState() + + if let thm = UIGraphicsGetImageFromCurrentImageContext() { + UIGraphicsEndImageContext(); + return thm; + } + } + } + } + + return UIImage() } func getThumbnail(_ url:URL, pageNumber:Int) -> UIImage {