Skip to content

Commit aed7206

Browse files
Merge pull request #598 from C4Framework/593-Render
Adds rendering to C4View and C4Shape
2 parents af2b468 + 0d9b9a5 commit aed7206

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Diff for: C4/UI/C4View+Render.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright © 2014 C4
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without limitation the
6+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
// sell copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions: The above copyright
9+
// notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18+
// IN THE SOFTWARE.
19+
20+
import UIKit
21+
22+
public extension C4View {
23+
public func render() -> C4Image? {
24+
guard let l = layer else {
25+
print("Could not retrieve layer for current object: \(self)")
26+
return nil
27+
}
28+
UIGraphicsBeginImageContextWithOptions(CGSize(size), false, UIScreen.mainScreen().scale)
29+
let context = UIGraphicsGetCurrentContext()!
30+
l.renderInContext(context)
31+
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
32+
UIGraphicsEndImageContext();
33+
return C4Image(uiimage: uiimage)
34+
}
35+
}
36+
37+
public extension C4Shape {
38+
public override func render() -> C4Image? {
39+
var s = CGSize(size)
40+
var inset: CGFloat = 0
41+
if lineWidth > 0 && strokeColor?.alpha > 0.0 {
42+
inset = CGFloat(lineWidth/2.0)
43+
s = CGRectInset(CGRect(frame), -inset, -inset).size
44+
}
45+
46+
let scale = CGFloat(UIScreen.mainScreen().scale)
47+
UIGraphicsBeginImageContextWithOptions(s, false, scale)
48+
let context = UIGraphicsGetCurrentContext()!
49+
CGContextTranslateCTM(context, CGFloat(-bounds.origin.x)+inset, CGFloat(-bounds.origin.y)+inset)
50+
shapeLayer.renderInContext(context)
51+
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
52+
UIGraphicsEndImageContext();
53+
return C4Image(uiimage: uiimage)
54+
}
55+
}

Diff for: C4iOS.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
1F754DCB1C48EE600036D39F /* C4ImageLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F754DCA1C48EE600036D39F /* C4ImageLayer.swift */; };
1111
1F8252CC1BB3C65E0090E98A /* C4Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8252CB1BB3C65E0090E98A /* C4Timer.swift */; };
12+
1F8A2FEB1C4C24FD0087063A /* C4View+Render.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */; };
1213
61BBAF631BC372BD00A03FD0 /* C4StoredAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61BBAF621BC372BD00A03FD0 /* C4StoredAnimation.swift */; };
1314
61BBAF651BC38C1200A03FD0 /* C4View+KeyValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61BBAF641BC38C1200A03FD0 /* C4View+KeyValues.swift */; };
1415
A94E3BD419E0E9390039A9C4 /* C4.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 614F824319DB5ED3001DF1D4 /* C4.framework */; };
@@ -125,6 +126,7 @@
125126
/* Begin PBXFileReference section */
126127
1F754DCA1C48EE600036D39F /* C4ImageLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4ImageLayer.swift; sourceTree = "<group>"; };
127128
1F8252CB1BB3C65E0090E98A /* C4Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4Timer.swift; sourceTree = "<group>"; };
129+
1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "C4View+Render.swift"; sourceTree = "<group>"; };
128130
1F8CB1D91A05781700A20783 /* C4RectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4RectTests.swift; sourceTree = "<group>"; };
129131
614F824319DB5ED3001DF1D4 /* C4.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = C4.framework; sourceTree = BUILT_PRODUCTS_DIR; };
130132
614F824E19DB5ED4001DF1D4 /* C4Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = C4Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -360,6 +362,7 @@
360362
A9D4F69A1B534F9F00F937AB /* C4View+Border.swift */,
361363
A9D4F69B1B534F9F00F937AB /* C4View+Shadow.swift */,
362364
A9D4F69C1B534F9F00F937AB /* C4View.swift */,
365+
1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */,
363366
61BBAF641BC38C1200A03FD0 /* C4View+KeyValues.swift */,
364367
A9D4F69D1B534F9F00F937AB /* C4ViewAnimation.swift */,
365368
A9D4F69E1B534F9F00F937AB /* C4Wedge.swift */,
@@ -584,6 +587,7 @@
584587
A9D4F6AD1B534F9F00F937AB /* C4Color.swift in Sources */,
585588
A9D4F6C51B534F9F00F937AB /* C4Image+Filter.swift in Sources */,
586589
A9D4F6BF1B534F9F00F937AB /* C4Curve.swift in Sources */,
590+
1F8A2FEB1C4C24FD0087063A /* C4View+Render.swift in Sources */,
587591
A9D4F6BC1B534F9F00F937AB /* C4AudioPlayer.swift in Sources */,
588592
A9D4F6B21B534F9F00F937AB /* C4Path.swift in Sources */,
589593
A9D4F6D71B534F9F00F937AB /* C4View.swift in Sources */,

0 commit comments

Comments
 (0)