Я хочу создать приложение, в котором пользователь может рисовать на экране, а затем сохранять свои рисунки. Ниже мой UIView
Жеребьевка код:Как сохранить чертеж CoreGraphics в Swift
import UIKit
class DrawView: UIView {
var lines: [Line] = [] // Line is a custom class, shown below
var lastPoint: CGPoint!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
lastPoint = touches.anyObject()?.locationInView(self)
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var newPoint = touches.anyObject()?.locationInView(self)
lines.append(Line(start: lastPoint, end: newPoint!))
lastPoint = newPoint
self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextBeginPath(context)
CGContextSetLineCap(context, kCGLineCapRound)
for line in lines {
CGContextMoveToPoint(context, line.start.x, line.start.y)
CGContextAddLineToPoint(context, line.end.x, line.end.y)
}
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
CGContextSetLineWidth(context, 5)
CGContextStrokePath(context)
}
}
Код для класса Line:
import UIKit
class Line {
var start: CGPoint
var end: CGPoint
init(start _start: CGPoint, end _end: CGPoint) {
start = _start
end = _end
}
}
Теперь я хочу, чтобы иметь возможность сохранить этот рисунок, используя NSUserDefaults
. Как мне это сделать?
Спасибо! Большая помощь. – brimstone