Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drawing Shapes to a PDF
// Copyright © 2014 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
import UIKit
import C4
class ViewController: CanvasController {
var pdfContext: CGContext!
var shapes: [Shape]!
override func setup() {
shapes = [Shape]()
for i in 0..<10 {
let center = Point(Double(i) * 30.0, Double(i) * 30.0)
let circle = Circle(center: center, radius: 30.0)
circle.fillColor = Color(red: random01(), green: random01(), blue: random01(), alpha: 1.0)
circle.lineWidth = 1 + 9 * random01()
canvas.add(circle)
shapes.append(circle)
}
draw(shapes: shapes, toPDFWithFileName: "hello.pdf")
}
func draw(shapes: [Shape], toPDFWithFileName fileName: String) {
let targetURL = dirtyDesktopDirectory?.appendingPathComponent(fileName)
guard let p = targetURL?.path else {
print("Could not retrieve path for \(targetURL)")
return
}
UIGraphicsBeginPDFContextToFile(p, CGRect(), nil)
UIGraphicsBeginPDFPageWithInfo(canvas.view.frame, nil)
for shape in shapes {
let bez = UIBezierPath(cgPath: shape.path!.CGPath)
bez.apply(CGAffineTransform(translationX: CGFloat(shape.origin.x), y: CGFloat(shape.origin.y)))
var color = UIColor.clear
if shape.fillColor != nil {
color = UIColor(shape.fillColor!)!
}
color.setFill()
bez.fill()
color = UIColor.clear
if shape.strokeColor != nil {
color = UIColor(shape.strokeColor!)!
}
bez.lineWidth = CGFloat(shape.lineWidth)
color.setStroke()
bez.stroke()
//ADD OTHER SHAPE DRAWING OPTIONS HERE
}
UIGraphicsEndPDFContext()
}
var dirtyDesktopDirectory: NSURL? {
let dir = FileManager.SearchPathDirectory.desktopDirectory
let dom = FileManager.SearchPathDomainMask.allDomainsMask
let paths = NSSearchPathForDirectoriesInDomains(dir, dom, true)
if let p = paths.first {
print(p) //non-dirty would be to get an accurate value for p
return NSURL(fileURLWithPath: "/Users/travis/Desktop") //<<deeerrrty – put your comp's name where mine is
}
return nil
}
//best to use this, if you're shipping an app, you can access it in iTunes
var applicationDocumentsDirectory: NSURL {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.endIndex-1] as NSURL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment