Skip to content

Commit 1a92841

Browse files
committed
Various optimizations.
1 parent 0a989fd commit 1a92841

File tree

9 files changed

+34
-33
lines changed

9 files changed

+34
-33
lines changed

FanOfAscii/Modules/BookCore.playgroundmodule/Sources/Core/RawImage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public struct Pixel {
1414
return (Double(red) + Double(green) + Double(blue)) / 3.0
1515
}()
1616

17+
lazy var uiColor: UIColor = {
18+
return UIColor(
19+
red: CGFloat(red) / 255.0,
20+
green: CGFloat(green) / 255.0,
21+
blue: CGFloat(blue) / 255.0,
22+
alpha: CGFloat(alpha) / 255.0)
23+
}()
24+
1725
}
1826

1927
public struct RgbaHistogram {

FanOfAscii/Modules/BookCore.playgroundmodule/Sources/UserInterface/Chapters/01-FanOfAscii/02-HowImagesComposed/Views/MagnifierView.swift

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ class MagnifierView: UIView {
1717

1818
var image: UIImage = UIImage() {
1919
didSet {
20-
if let provider = image.cgImage?.dataProvider {
21-
imageRawData = provider.data
22-
}
20+
rawImage = RawImage(uiImage: image)
2321
self.setNeedsDisplay()
2422
}
2523
}
2624

27-
private var imageRawData: CFData?
25+
private var rawImage: RawImage?
2826

2927
override func awakeFromNib() {
3028
super.awakeFromNib()
@@ -39,8 +37,8 @@ class MagnifierView: UIView {
3937

4038
override func draw(_ rect: CGRect) {
4139
guard
42-
let center = magnificationCenter,
43-
let rawData = imageRawData else {
40+
let center = magnificationCenter,
41+
let rawImage = rawImage else {
4442
return
4543
}
4644

@@ -56,7 +54,9 @@ class MagnifierView: UIView {
5654
for x in 0..<samplePixels {
5755
let ix = x + Int(croppingRect.minX)
5856
let iy = y + Int(croppingRect.minY)
59-
let pixelColor: UIColor = pixelColorAt(x: ix, y: iy, rawData: rawData, size: image.size) ?? .black
57+
58+
var pixel = rawImage.pixelAt(x: ix, y: iy)
59+
let pixelColor: UIColor = pixel?.uiColor ?? .black
6060

6161
context.setFillColor(pixelColor.cgColor)
6262
context.fill(CGRect(x: x * pixelSize, y: y * pixelSize, width: pixelSize, height: pixelSize))
@@ -75,24 +75,6 @@ class MagnifierView: UIView {
7575
context.stroke(CGRect(x: 0, y: mid * pixelSize, width: pixelSize * samplePixels, height: pixelSize))
7676
}
7777

78-
func pixelColorAt(x: Int, y: Int, rawData: CFData, size: CGSize) -> UIColor? {
79-
if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) {
80-
return nil
81-
}
82-
83-
let data = CFDataGetBytePtr(rawData)
84-
85-
let numberOfComponents = 4
86-
let pixelData = ((Int(size.width) * y) + x) * numberOfComponents
87-
88-
let r = CGFloat(data![pixelData]) / 255.0
89-
let g = CGFloat(data![pixelData + 1]) / 255.0
90-
let b = CGFloat(data![pixelData + 2]) / 255.0
91-
let a = CGFloat(data![pixelData + 3]) / 255.0
92-
93-
return UIColor(red: r, green: g, blue: b, alpha: a)
94-
}
95-
9678
}
9779

9880

FanOfAscii/Modules/BookCore.playgroundmodule/Sources/UserInterface/Chapters/01-FanOfAscii/04-Asciification/AsciificationLiveViewController.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,21 @@ class AsciificationLiveViewController: BaseViewController {
6262
} else if let image = sourceImage {
6363
updateShowcaseImage(image: image)
6464
}
65-
66-
imageScaleButton.isHidden = !(shrinkButton.state == .selected)
65+
imageScaleButton.isHidden = !(shrinkButton.state == .selected && asciificationButton.state == .normal)
6766
}
6867

6968
private func saveCurrentImage() {
70-
69+
if let imageToSave = showcaseImageView.image {
70+
UIImageWriteToSavedPhotosAlbum(imageToSave, self, #selector(didSaveImage(image:didFinishSavingWithError:contextInfo:)), nil)
71+
}
7172
}
7273

7374
override func didSelectImage(image: UIImage, pickerController: ImagePickerViewController) {
7475
super.didSelectImage(image: image, pickerController: pickerController)
76+
if asciificationButton.state == .selected {
77+
asciificationButton.state = .normal
78+
saveButton.state = .disabled
79+
}
7580
updatePreprocessedImage()
7681
updateImageForButtonStates()
7782
}
@@ -96,6 +101,12 @@ class AsciificationLiveViewController: BaseViewController {
96101
updateImageForButtonStates()
97102
}
98103

104+
@objc func didSaveImage(image: UIImage, didFinishSavingWithError error: Error, contextInfo: UnsafeMutableRawPointer?) {
105+
let alert = UIAlertController(title: "Congratulations!", message: "Your ASCII art has been save to photo album.", preferredStyle: .alert)
106+
alert.addAction(UIAlertAction(title: "Dismiss", style: .default))
107+
self.present(alert, animated: true, completion: nil)
108+
}
109+
99110
}
100111

101112
extension AsciificationLiveViewController: ScaleModeButtonDelegate {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"images" : [
33
{
44
"idiom" : "universal",
5-
"filename" : "button-histogram-export.pdf"
5+
"filename" : "tool-button-export.pdf"
66
}
77
],
88
"info" : {

FanOfAscii/PrivateResources/LiveView.storyboard

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<subviews>
104104
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ehc-7p-QnD">
105105
<rect key="frame" x="0.0" y="0.0" width="48" height="48"/>
106-
<state key="normal" image="toolbar/button-histogram-shrink"/>
106+
<state key="normal" image="toolbar/button-shrink"/>
107107
<userDefinedRuntimeAttributes>
108108
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
109109
<integer key="value" value="14"/>
@@ -175,7 +175,7 @@
175175
<subviews>
176176
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="X3B-qz-drT">
177177
<rect key="frame" x="0.0" y="0.0" width="48" height="48"/>
178-
<state key="normal" image="toolbar/button-histogram-export"/>
178+
<state key="normal" image="toolbar/button-save"/>
179179
<userDefinedRuntimeAttributes>
180180
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
181181
<integer key="value" value="14"/>
@@ -886,11 +886,11 @@
886886
<image name="toolbar/button-contrast" width="48" height="48"/>
887887
<image name="toolbar/button-grayscale" width="48" height="48"/>
888888
<image name="toolbar/button-green" width="48" height="48"/>
889-
<image name="toolbar/button-histogram-export" width="48" height="48"/>
890889
<image name="toolbar/button-histogram-luma" width="48" height="48"/>
891-
<image name="toolbar/button-histogram-shrink" width="48" height="48"/>
892890
<image name="toolbar/button-magnifier" width="48" height="48"/>
893891
<image name="toolbar/button-red" width="48" height="48"/>
892+
<image name="toolbar/button-save" width="48" height="48"/>
893+
<image name="toolbar/button-shrink" width="48" height="48"/>
894894
<image name="toolbar/icon-tick" width="24" height="24"/>
895895
</resources>
896896
</document>

LiveViewTestApp/Info.plist

97 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)