@@ -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