-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoodleView.swift
More file actions
127 lines (100 loc) · 3.85 KB
/
Copy pathDoodleView.swift
File metadata and controls
127 lines (100 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// DoodleView.swift
// doodle grid client
//
// Created by Vito Genovese on 8/7/18.
// Copyright © 2018 Vito Genovese. All rights reserved.
//
import UIKit
import os.log
class DoodleView: UIImageView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
var cleanImage: UIImage?
var cleanBuf: [UInt8]?
var penColor: UIColor = UIColor.red
var bgColor: UIColor = UIColor.red
var pixelBuf: [UInt8] = []
let destImageSize = CGSize(width: 16, height: 16)
let bufBytesPerPixel = 3
override func awakeFromNib() {
super.awakeFromNib()
pixelBuf = [UInt8](repeating: 0, count:
(Int)(destImageSize.width) *
(Int)(destImageSize.height) *
bufBytesPerPixel)
}
func clear() {
let i = self.image!
let r = (UInt8)(255 * bgColor.cgColor.components![0])
let g = (UInt8)(255 * bgColor.cgColor.components![1])
let b = (UInt8)(255 * bgColor.cgColor.components![2])
for y in 0..<((Int)(destImageSize.width)) {
for x in 0..<((Int)(destImageSize.height)) {
let idx = bufBytesPerPixel * (x + (y * 16))
pixelBuf[idx] = r
pixelBuf[idx + 1] = g
pixelBuf[idx + 2] = b
}
}
UIGraphicsBeginImageContext(i.size)
i.draw(at: CGPoint.zero)
bgColor.setFill()
UIRectFill(CGRect(origin: .zero, size: i.size))
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
cleanImage = UIImage(cgImage: self.image!.cgImage!)
cleanBuf = [UInt8](pixelBuf)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let i = self.image!
UIGraphicsBeginImageContext(i.size)
let ctx = UIGraphicsGetCurrentContext()
ctx?.interpolationQuality = .none
i.draw(at: CGPoint.zero)
penColor.setFill()
let scale = (Int)(frame.size.width / destImageSize.width)
let uiScale = (Int)(i.size.width / frame.size.width)
let r = (UInt8)(255 * penColor.cgColor.components![0])
let g = (UInt8)(255 * penColor.cgColor.components![1])
let b = (UInt8)(255 * penColor.cgColor.components![2])
for t in touches {
let loc = t.location(in: self)
let x_loc = Int(loc.x)
let y_loc = Int(loc.y)
let x_idx = x_loc / scale
let y_idx = (y_loc / scale) * 16
let idx = bufBytesPerPixel * (x_idx + y_idx)
if idx >= pixelBuf.count {
continue
}
pixelBuf[idx] = r
pixelBuf[idx + 1] = g
pixelBuf[idx + 2] = b
let scaleMod = scale * uiScale
let uiX = (uiScale * x_loc) - ((uiScale * x_loc) % scaleMod)
let uiY = (uiScale * y_loc) - ((uiScale * y_loc) % scaleMod)
let rect = CGRect(x: uiX,
y: uiY,
width: scale * uiScale,
height: scale * uiScale)
UIRectFill(rect)
}
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
cancelLastDraw()
}
func cancelLastDraw() {
self.image = cleanImage
self.pixelBuf = cleanBuf!
}
}