Skip to content

Commit 9ecb6f1

Browse files
author
Robin Oster
committed
Treat Optionals a little bit safer.
1 parent 2d02b35 commit 9ecb6f1

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

ROThumbnailGenerator.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
Pod::Spec.new do |spec|
1313
spec.name = 'ROThumbnailGenerator'
14-
spec.version = '2.1.1'
14+
spec.version = '2.1.2'
1515
spec.license = { :type => 'MIT' }
1616
spec.homepage = 'https://github.com/prine/ROThumbnailGenerator'
1717
spec.authors = { 'Robin Oster' => '[email protected]' }
1818
spec.summary = 'Creates thumbnails of different file types very easily'
19-
spec.source = { :git => 'https://github.com/prine/ROThumbnailGenerator.git', :tag => "2.1.1" }
19+
spec.source = { :git => 'https://github.com/prine/ROThumbnailGenerator.git', :tag => "2.1.2" }
2020
spec.source_files = 'Source/**/*'
2121
spec.framework = 'SystemConfiguration'
2222
spec.ios.deployment_target = '8.4'

ROThumbnailGenerator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
CB3438B41B7B6A8900EEB5DB /* TestPdf.pdf in Resources */ = {isa = PBXBuildFile; fileRef = CB3438B31B7B6A8900EEB5DB /* TestPdf.pdf */; };
2424
CB3438BC1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec in Resources */ = {isa = PBXBuildFile; fileRef = CB3438BA1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec */; };
2525
CB3438BE1B7B790800EEB5DB /* ROThumbnail.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB3438BD1B7B790800EEB5DB /* ROThumbnail.swift */; };
26+
CB48C11A1DAD440800B7CD15 /* TestPdf2.pdf in Resources */ = {isa = PBXBuildFile; fileRef = CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */; };
2627
/* End PBXBuildFile section */
2728

2829
/* Begin PBXContainerItemProxy section */
@@ -57,6 +58,7 @@
5758
CB3438B91B7B71BB00EEB5DB /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
5859
CB3438BA1B7B71BB00EEB5DB /* ROThumbnailGenerator.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ROThumbnailGenerator.podspec; sourceTree = SOURCE_ROOT; };
5960
CB3438BD1B7B790800EEB5DB /* ROThumbnail.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ROThumbnail.swift; path = Source/ROThumbnail.swift; sourceTree = SOURCE_ROOT; };
61+
CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = TestPdf2.pdf; sourceTree = "<group>"; };
6062
/* End PBXFileReference section */
6163

6264
/* Begin PBXFrameworksBuildPhase section */
@@ -117,6 +119,7 @@
117119
CB3438AF1B7B6A3C00EEB5DB /* TestMovie.mov */,
118120
CB3438B11B7B6A4900EEB5DB /* TestImage.png */,
119121
CB3438B31B7B6A8900EEB5DB /* TestPdf.pdf */,
122+
CB48C1191DAD440800B7CD15 /* TestPdf2.pdf */,
120123
CB3438791B7B3BAF00EEB5DB /* Info.plist */,
121124
);
122125
name = "Supporting Files";
@@ -238,6 +241,7 @@
238241
isa = PBXResourcesBuildPhase;
239242
buildActionMask = 2147483647;
240243
files = (
244+
CB48C11A1DAD440800B7CD15 /* TestPdf2.pdf in Resources */,
241245
CB3438B21B7B6A4900EEB5DB /* TestImage.png in Resources */,
242246
CB3438801B7B3BAF00EEB5DB /* Main.storyboard in Resources */,
243247
CB3438B01B7B6A3C00EEB5DB /* TestMovie.mov in Resources */,

ROThumbnailGenerator/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>2.0.1</string>
18+
<string>2.1.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>15265</string>
22+
<string>16295</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UILaunchStoryboardName</key>

ROThumbnailGenerator/TestPdf2.pdf

1.51 MB
Binary file not shown.

ROThumbnailGenerator/ViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class ViewController: UIViewController {
2222
let imageURL = Bundle.main.url(forResource: "TestImage", withExtension: "png")
2323
testImage.image = thumbnail.getThumbnail(imageURL!)
2424

25-
let documentURL = Bundle.main.url(forResource: "TestPdf", withExtension: "pdf")
25+
// let documentURL = Bundle.main.url(forResource: "TestPdf", withExtension: "pdf")
26+
let documentURL = Bundle.main.url(forResource: "TestPdf2", withExtension: "pdf")
2627
testPdf.image = thumbnail.getThumbnail(documentURL!)
2728

2829
let movieURL = Bundle.main.url(forResource: "TestMovie", withExtension: "mov")

Source/PDFThumbnailGenerator.swift

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,42 @@ class PDFThumbnailGenerator : ROThumbnailGenerator {
1313
var supportedExtensions:Array<String> = ["pdf"]
1414

1515
func getThumbnail(_ url:URL, pageNumber:Int, width:CGFloat) -> UIImage {
16-
let pdf:CGPDFDocument = CGPDFDocument(url as CFURL)!;
17-
18-
let firstPage = pdf.page(at: pageNumber)
19-
20-
var pageRect:CGRect = firstPage!.getBoxRect(CGPDFBox.mediaBox);
21-
let pdfScale:CGFloat = width/pageRect.size.width;
22-
pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale);
23-
pageRect.origin = CGPoint.zero;
24-
25-
UIGraphicsBeginImageContext(pageRect.size);
26-
27-
let context:CGContext = UIGraphicsGetCurrentContext()!;
28-
29-
// White BG
30-
context.setFillColor(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0);
31-
context.fill(pageRect);
32-
33-
context.saveGState();
34-
35-
// Next 3 lines makes the rotations so that the page look in the right direction
36-
context.translateBy(x: 0.0, y: pageRect.size.height);
37-
context.scaleBy(x: 1.0, y: -1.0);
38-
context.concatenate((firstPage?.getDrawingTransform(CGPDFBox.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))!);
39-
40-
context.drawPDFPage(firstPage!);
41-
context.restoreGState();
42-
43-
let thm:UIImage = UIGraphicsGetImageFromCurrentImageContext()!;
44-
45-
UIGraphicsEndImageContext();
46-
return thm;
47-
16+
if let pdf:CGPDFDocument = CGPDFDocument(url as CFURL) {
17+
18+
if let firstPage = pdf.page(at: pageNumber) {
19+
20+
var pageRect:CGRect = firstPage.getBoxRect(CGPDFBox.mediaBox)
21+
let pdfScale:CGFloat = width/pageRect.size.width
22+
pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale)
23+
pageRect.origin = CGPoint.zero
24+
25+
UIGraphicsBeginImageContext(pageRect.size)
26+
27+
if let context:CGContext = UIGraphicsGetCurrentContext() {
28+
29+
// White BG
30+
context.setFillColor(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0)
31+
context.fill(pageRect)
32+
33+
context.saveGState()
34+
35+
// Next 3 lines makes the rotations so that the page look in the right direction
36+
context.translateBy(x: 0.0, y: pageRect.size.height)
37+
context.scaleBy(x: 1.0, y: -1.0)
38+
context.concatenate((firstPage.getDrawingTransform(CGPDFBox.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true)))
39+
40+
context.drawPDFPage(firstPage)
41+
context.restoreGState()
42+
43+
if let thm = UIGraphicsGetImageFromCurrentImageContext() {
44+
UIGraphicsEndImageContext();
45+
return thm;
46+
}
47+
}
48+
}
49+
}
50+
51+
return UIImage()
4852
}
4953

5054
func getThumbnail(_ url:URL, pageNumber:Int) -> UIImage {

0 commit comments

Comments
 (0)