Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/11 #12

Merged
merged 3 commits into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion PDFGenerator/PDFGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum PDFPage {
case View(UIView)
case Image(UIImage)
case ImagePath(String)
case Binary(NSData)
case ImageRef(CGImage)

static func pages(views: [UIView]) -> [PDFPage] {
return views.map { .View($0) }
Expand All @@ -45,7 +47,7 @@ public enum PDFPage {
*/
public enum PDFGenerateError: ErrorType {
case ZeroSizeView(UIView)
case ImageLoadFailed(String)
case ImageLoadFailed(AnyObject)
case EmptyOutputPath
case EmptyPage
}
Expand Down Expand Up @@ -301,6 +303,14 @@ private extension PDFGenerator {
try autoreleasepool {
try ip.to_image().renderPDFPage()
}
case .Binary(let data):
try autoreleasepool {
try data.to_image().renderPDFPage()
}
case .ImageRef(let cgImage):
try autoreleasepool {
try cgImage.to_image().renderPDFPage()
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion PDFGenerator/PDFPageRenderable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,30 @@ extension UIImage: PDFPageRenderable {
}
}

extension String {
protocol UIImageConvertible {
func to_image() throws -> UIImage
}

extension String: UIImageConvertible {
func to_image() throws -> UIImage{
guard let image = UIImage(contentsOfFile: self) else{
throw PDFGenerateError.ImageLoadFailed(self)
}
return image
}
}

extension NSData: UIImageConvertible {
func to_image() throws -> UIImage {
guard let image = UIImage(data: self) else {
throw PDFGenerateError.ImageLoadFailed(self)
}
return image
}
}

extension CGImage: UIImageConvertible {
func to_image() throws -> UIImage {
return UIImage(CGImage: self)
}
}
25 changes: 19 additions & 6 deletions PDFGeneratorTests/PDFGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class PDFGeneratorTests: XCTestCase {
let p2 = PDFPage.Image(Mock.image(Mock.ImageName.testImage1))
let p3 = PDFPage.ImagePath(Mock.imagePath(Mock.ImageName.testImage1))
let p4 = PDFPage.WhitePage(CGSizeMake(100, 100))
let p5 = PDFPage.ImageRef(Mock.image(Mock.ImageName.testImage1).CGImage!)
let p6 = PDFPage.Binary(UIImagePNGRepresentation(Mock.image(Mock.ImageName.testImage1))!)

let path1 = PDFfilePath("test_sample1.pdf")
_ = try? PDFGenerator.generate(p1, outputPath: path1)
Expand All @@ -145,6 +147,7 @@ class PDFGeneratorTests: XCTestCase {
XCTAssertNotNil(try? PDFGenerator.generate(p1))
XCTAssertNotNil(try? PDFGenerator.generate([p2]))
XCTAssertNotNil(try? PDFGenerator.generate([p3, p4]))
XCTAssertNotNil(try? PDFGenerator.generate([p5, p6]))

}

Expand Down Expand Up @@ -328,21 +331,21 @@ class PDFGeneratorTests: XCTestCase {
let path = PDFfilePath("test_sample4.pdf")
try PDFGenerator.generate(wrongImagePath, outputPath: path)
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
do {
_ = try PDFGenerator.generate(wrongImagePath)
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
do {
_ = try PDFGenerator.generate([wrongImagePath])
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
Expand All @@ -352,21 +355,31 @@ class PDFGeneratorTests: XCTestCase {
let path = PDFfilePath("test_sample5.pdf")
try PDFGenerator.generate(wrongImagePathPage, outputPath: path)
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
do {
_ = try PDFGenerator.generate(wrongImagePathPage)
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
do {
_ = try PDFGenerator.generate([wrongImagePathPage])
} catch PDFGenerateError.ImageLoadFailed(let ip) {
XCTAssertEqual(wrongImagePath, ip)
XCTAssertEqual(wrongImagePath, ip as? String)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}

let wrongData = NSData()

do {
_ = try PDFGenerator.generate(PDFPage.Binary(wrongData))
} catch PDFGenerateError.ImageLoadFailed(let data) {
XCTAssertEqual(wrongData, data as? NSData)
} catch (let e) {
XCTFail("Unknown or wrong error occurred.\(e)")
}
Expand Down