Skip to content

Commit 6c897c3

Browse files
committed
Gradient
1 parent 580fe04 commit 6c897c3

File tree

2 files changed

+19
-75
lines changed

2 files changed

+19
-75
lines changed

Examples/Basic/Basic.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
D9126F0E24A6C19300859CC5 /* gradient-apple.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9126F0D24A6C19200859CC5 /* gradient-apple.svg */; };
1313
D918D26D22122A7300097C9A /* arc2.svg in Resources */ = {isa = PBXBuildFile; fileRef = D918D26C22122A7300097C9A /* arc2.svg */; };
1414
D91C3E43224ED9A4001BCDAE /* gradient-gratification.svg in Resources */ = {isa = PBXBuildFile; fileRef = D91C3E42224ED9A4001BCDAE /* gradient-gratification.svg */; };
15+
D92A71FE27818B860001630A /* simple.svg in Resources */ = {isa = PBXBuildFile; fileRef = D92A71FC27818B860001630A /* simple.svg */; };
16+
D92A71FF27818B860001630A /* pattern-bounding.svg in Resources */ = {isa = PBXBuildFile; fileRef = D92A71FD27818B860001630A /* pattern-bounding.svg */; };
1517
D938A203268307670051E18B /* pattern-rotate.svg in Resources */ = {isa = PBXBuildFile; fileRef = D938A202268307670051E18B /* pattern-rotate.svg */; };
1618
D945F5AA225220F900FAAAC4 /* mask-composite-2.svg in Resources */ = {isa = PBXBuildFile; fileRef = D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */; };
1719
D945F5AB225220F900FAAAC4 /* mask-composite-3.svg in Resources */ = {isa = PBXBuildFile; fileRef = D945F5A9225220F900FAAAC4 /* mask-composite-3.svg */; };
@@ -63,6 +65,8 @@
6365
D9126F0D24A6C19200859CC5 /* gradient-apple.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "gradient-apple.svg"; sourceTree = "<group>"; };
6466
D918D26C22122A7300097C9A /* arc2.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = arc2.svg; sourceTree = "<group>"; };
6567
D91C3E42224ED9A4001BCDAE /* gradient-gratification.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "gradient-gratification.svg"; sourceTree = "<group>"; };
68+
D92A71FC27818B860001630A /* simple.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = simple.svg; sourceTree = "<group>"; };
69+
D92A71FD27818B860001630A /* pattern-bounding.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "pattern-bounding.svg"; sourceTree = "<group>"; };
6670
D938A202268307670051E18B /* pattern-rotate.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "pattern-rotate.svg"; sourceTree = "<group>"; };
6771
D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "mask-composite-2.svg"; sourceTree = "<group>"; };
6872
D945F5A9225220F900FAAAC4 /* mask-composite-3.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "mask-composite-3.svg"; sourceTree = "<group>"; };
@@ -141,7 +145,6 @@
141145
D9ACD782220FDDE0009717CF /* Samples */ = {
142146
isa = PBXGroup;
143147
children = (
144-
D938A202268307670051E18B /* pattern-rotate.svg */,
145148
D9ACD78D220FDDE0009717CF /* arc.svg */,
146149
D918D26C22122A7300097C9A /* arc2.svg */,
147150
D945F5AC225241A100FAAAC4 /* blend.svg */,
@@ -161,10 +164,13 @@
161164
D9ACD78C220FDDE0009717CF /* mask.svg */,
162165
D90523442217927F00B3B94F /* mystery.svg */,
163166
D9ACD784220FDDE0009717CF /* path.svg */,
167+
D92A71FD27818B860001630A /* pattern-bounding.svg */,
168+
D938A202268307670051E18B /* pattern-rotate.svg */,
164169
D9D9EB2C224A3BCA006CF83C /* pattern.svg */,
165170
D9ACD78E220FDDE0009717CF /* quad.svg */,
166171
D991955724BAF8E200E58925 /* rings.svg */,
167172
D9ACD789220FDDE0009717CF /* shapes.svg */,
173+
D92A71FC27818B860001630A /* simple.svg */,
168174
D9ACD78B220FDDE0009717CF /* starry.svg */,
169175
D9494F43224C846D0015ACE1 /* thats-no-moon.svg */,
170176
D9ACD786220FDDE0009717CF /* transform.svg */,
@@ -253,13 +259,15 @@
253259
01BA4DA826820DE8001FF7B5 /* gradient-gratification-p3.svg in Resources */,
254260
D9ACD797220FDDE0009717CF /* shapes.svg in Resources */,
255261
D9ACD799220FDDE0009717CF /* starry.svg in Resources */,
262+
D92A71FF27818B860001630A /* pattern-bounding.svg in Resources */,
256263
D9ACD791220FDDE0009717CF /* use.svg in Resources */,
257264
D91C3E43224ED9A4001BCDAE /* gradient-gratification.svg in Resources */,
258265
D9ACD793220FDDE0009717CF /* viewbox.svg in Resources */,
259266
D9618470220FDD1200C59D9B /* LaunchScreen.storyboard in Resources */,
260267
D9ACD798220FDDE0009717CF /* lines.svg in Resources */,
261268
D918D26D22122A7300097C9A /* arc2.svg in Resources */,
262269
D9ACD79B220FDDE0009717CF /* arc.svg in Resources */,
270+
D92A71FE27818B860001630A /* simple.svg in Resources */,
263271
D961846D220FDD1200C59D9B /* Assets.xcassets in Resources */,
264272
D938A203268307670051E18B /* pattern-rotate.svg in Resources */,
265273
D9ACD79D220FDDE0009717CF /* display.svg in Resources */,

Examples/Basic/Sources/ViewController.swift

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,17 @@ class ViewController: UIViewController {
4646

4747
@objc
4848
func didTap() {
49-
guard let size = imageViewIfLoaded?.image?.size else { return }
50-
51-
switch size {
52-
case CGSize(width: 480, height: 352):
53-
imageViewIfLoaded?.image = Image(named: "gradient-gratification-p3.svg")?.rasterize(with: CGSize(width: 960, height: 704))
49+
guard let contentMode = imageViewIfLoaded?.contentMode else { return }
50+
switch contentMode {
51+
case .center:
52+
imageViewIfLoaded?.contentMode = .scaleAspectFit
53+
case .scaleAspectFit:
54+
imageViewIfLoaded?.contentMode = .scaleAspectFill
55+
case .scaleAspectFill:
56+
imageViewIfLoaded?.contentMode = .center
5457
default:
55-
imageViewIfLoaded?.image = Image(named: "gradient-gratification-p3.svg")?.rasterize(with: CGSize(width: 480, height: 352))
58+
imageViewIfLoaded?.contentMode = .center
5659
}
57-
// guard let contentMode = imageViewIfLoaded?.contentMode else { return }
58-
// switch contentMode {
59-
// case .center:
60-
// imageViewIfLoaded?.contentMode = .scaleAspectFit
61-
// case .scaleAspectFit:
62-
// imageViewIfLoaded?.contentMode = .scaleAspectFill
63-
// case .scaleAspectFill:
64-
// imageViewIfLoaded?.contentMode = .center
65-
// default:
66-
// imageViewIfLoaded?.contentMode = .center
67-
// }
6860
}
6961

7062
var imageViewIfLoaded: UIImageView? {
@@ -73,12 +65,10 @@ class ViewController: UIViewController {
7365

7466
override func loadView() {
7567
let imageView = UIImageView(frame: UIScreen.main.bounds)
76-
imageView.image = .svgPatternRotate()
68+
imageView.image = Image(named: "gradient-gratification-p3.svg")?.rasterize()
7769
imageView.contentMode = .scaleAspectFit
7870
imageView.backgroundColor = .white
7971
self.view = imageView
80-
81-
print(CGTextRenderer.render(named: "pattern-rotate.svg")!)
8272
}
8373
}
8474

@@ -100,57 +90,3 @@ private extension Image {
10090
.takeUnretainedValue() as? UIImage
10191
}
10292
}
103-
104-
extension UIImage {
105-
static func svgPatternRotate(size: CGSize = CGSize(width: 256.0, height: 256.0)) -> UIImage {
106-
let f = UIGraphicsImageRendererFormat.preferred()
107-
f.opaque = false
108-
let scale = CGSize(width: size.width / 256.0, height: size.height / 256.0)
109-
return UIGraphicsImageRenderer(size: size, format: f).image {
110-
drawSVG(in: $0.cgContext, scale: scale)
111-
}
112-
}
113-
114-
private static func drawSVG(in ctx: CGContext, scale: CGSize) {
115-
let baseCTM = ctx.ctm
116-
ctx.scaleBy(x: scale.width, y: scale.height)
117-
let rgb = CGColorSpaceCreateDeviceRGB()
118-
let color1 = CGColor(colorSpace: rgb, components: [1.0, 0.98039216, 0.98039216, 1.0])!
119-
ctx.setFillColor(color1)
120-
ctx.fill(CGRect(x: 0.0, y: 0.0, width: 256.0, height: 256.0))
121-
ctx.saveGState()
122-
ctx.translateBy(x: 128.0, y: 128.0)
123-
ctx.rotate(by: 0.7853981)
124-
let patternDraw: CGPatternDrawPatternCallback = { _, ctx in
125-
let rgb = CGColorSpaceCreateDeviceRGB()
126-
let color1 = CGColor(colorSpace: rgb, components: [0.0, 0.5019608, 0.0, 1.0])!
127-
ctx.setFillColor(color1)
128-
ctx.fill(CGRect(x: 0.0, y: 0.0, width: 32.0, height: 32.0))
129-
let color2 = CGColor(colorSpace: rgb, components: [1.0, 0.0, 0.0, 1.0])!
130-
ctx.setFillColor(color2)
131-
ctx.fill(CGRect(x: 32.0, y: 0.0, width: 32.0, height: 32.0))
132-
let color3 = CGColor(colorSpace: rgb, components: [0.0, 0.0, 1.0, 1.0])!
133-
ctx.setFillColor(color3)
134-
ctx.fill(CGRect(x: 0.0, y: 32.0, width: 32.0, height: 32.0))
135-
let color4 = CGColor(colorSpace: rgb, components: [1.0, 0.7529412, 0.79607844, 1.0])!
136-
ctx.setFillColor(color4)
137-
ctx.fill(CGRect(x: 32.0, y: 32.0, width: 32.0, height: 32.0))
138-
}
139-
var patternCallback = CGPatternCallbacks(version: 0, drawPattern: patternDraw, releaseInfo: nil)
140-
let pattern = CGPattern(
141-
info: nil,
142-
bounds: CGRect(x: 0.0, y: 0.0, width: 64.0, height: 64.0),
143-
matrix: ctx.ctm.concatenating(baseCTM.inverted()),
144-
xStep: 64.0,
145-
yStep: 64.0,
146-
tiling: .constantSpacing,
147-
isColored: true,
148-
callbacks: &patternCallback
149-
)!
150-
ctx.setFillColorSpace(CGColorSpace(patternBaseSpace: nil)!)
151-
var patternAlpha : CGFloat = 1.0
152-
ctx.setFillPattern(pattern, colorComponents: &patternAlpha)
153-
ctx.fill(CGRect(x: -64.0, y: -64.0, width: 128.0, height: 128.0))
154-
ctx.restoreGState()
155-
}
156-
}

0 commit comments

Comments
 (0)