Skip to content

Commit

Permalink
Treat Optionals a little bit safer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Oster committed Oct 21, 2016
1 parent 2d02b35 commit 9ecb6f1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions ROThumbnailGenerator.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]' }
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'
Expand Down
4 changes: 4 additions & 0 deletions ROThumbnailGenerator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -117,6 +119,7 @@
CB3438AF1B7B6A3C00EEB5DB /* TestMovie.mov */,
CB3438B11B7B6A4900EEB5DB /* TestImage.png */,
CB3438B31B7B6A8900EEB5DB /* TestPdf.pdf */,
CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */,
CB3438791B7B3BAF00EEB5DB /* Info.plist */,
);
name = "Supporting Files";
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 2 additions & 2 deletions ROThumbnailGenerator/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0.1</string>
<string>2.1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>15265</string>
<string>16295</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
Binary file added ROThumbnailGenerator/TestPdf2.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion ROThumbnailGenerator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
68 changes: 36 additions & 32 deletions Source/PDFThumbnailGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,42 @@ class PDFThumbnailGenerator : ROThumbnailGenerator {
var supportedExtensions:Array<String> = ["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 {
Expand Down

0 comments on commit 9ecb6f1

Please sign in to comment.