|
| 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 | +} |
0 commit comments