Skip to content

Adds rendering to C4View and C4Shape #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions C4/UI/C4View+Render.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright © 2014 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

import UIKit

public extension C4View {
public func render() -> C4Image? {
guard let l = layer else {
print("Could not retrieve layer for current object: \(self)")
return nil
}
UIGraphicsBeginImageContextWithOptions(CGSize(size), false, UIScreen.mainScreen().scale)
let context = UIGraphicsGetCurrentContext()!
l.renderInContext(context)
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return C4Image(uiimage: uiimage)
}
}

public extension C4Shape {
public override func render() -> C4Image? {
var s = CGSize(size)
var inset: CGFloat = 0
if lineWidth > 0 && strokeColor?.alpha > 0.0 {
inset = CGFloat(lineWidth/2.0)
s = CGRectInset(CGRect(frame), -inset, -inset).size
}

let scale = CGFloat(UIScreen.mainScreen().scale)
UIGraphicsBeginImageContextWithOptions(s, false, scale)
let context = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(context, CGFloat(-bounds.origin.x)+inset, CGFloat(-bounds.origin.y)+inset)
shapeLayer.renderInContext(context)
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return C4Image(uiimage: uiimage)
}
}
4 changes: 4 additions & 0 deletions C4iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1F754DCB1C48EE600036D39F /* C4ImageLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F754DCA1C48EE600036D39F /* C4ImageLayer.swift */; };
1F8252CC1BB3C65E0090E98A /* C4Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8252CB1BB3C65E0090E98A /* C4Timer.swift */; };
1F8A2FEB1C4C24FD0087063A /* C4View+Render.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */; };
61BBAF631BC372BD00A03FD0 /* C4StoredAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61BBAF621BC372BD00A03FD0 /* C4StoredAnimation.swift */; };
61BBAF651BC38C1200A03FD0 /* C4View+KeyValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61BBAF641BC38C1200A03FD0 /* C4View+KeyValues.swift */; };
A94E3BD419E0E9390039A9C4 /* C4.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 614F824319DB5ED3001DF1D4 /* C4.framework */; };
Expand Down Expand Up @@ -125,6 +126,7 @@
/* Begin PBXFileReference section */
1F754DCA1C48EE600036D39F /* C4ImageLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4ImageLayer.swift; sourceTree = "<group>"; };
1F8252CB1BB3C65E0090E98A /* C4Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4Timer.swift; sourceTree = "<group>"; };
1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "C4View+Render.swift"; sourceTree = "<group>"; };
1F8CB1D91A05781700A20783 /* C4RectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C4RectTests.swift; sourceTree = "<group>"; };
614F824319DB5ED3001DF1D4 /* C4.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = C4.framework; sourceTree = BUILT_PRODUCTS_DIR; };
614F824E19DB5ED4001DF1D4 /* C4Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = C4Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -360,6 +362,7 @@
A9D4F69A1B534F9F00F937AB /* C4View+Border.swift */,
A9D4F69B1B534F9F00F937AB /* C4View+Shadow.swift */,
A9D4F69C1B534F9F00F937AB /* C4View.swift */,
1F8A2FEA1C4C24FD0087063A /* C4View+Render.swift */,
61BBAF641BC38C1200A03FD0 /* C4View+KeyValues.swift */,
A9D4F69D1B534F9F00F937AB /* C4ViewAnimation.swift */,
A9D4F69E1B534F9F00F937AB /* C4Wedge.swift */,
Expand Down Expand Up @@ -584,6 +587,7 @@
A9D4F6AD1B534F9F00F937AB /* C4Color.swift in Sources */,
A9D4F6C51B534F9F00F937AB /* C4Image+Filter.swift in Sources */,
A9D4F6BF1B534F9F00F937AB /* C4Curve.swift in Sources */,
1F8A2FEB1C4C24FD0087063A /* C4View+Render.swift in Sources */,
A9D4F6BC1B534F9F00F937AB /* C4AudioPlayer.swift in Sources */,
A9D4F6B21B534F9F00F937AB /* C4Path.swift in Sources */,
A9D4F6D71B534F9F00F937AB /* C4View.swift in Sources */,
Expand Down