Last active
October 27, 2015 11:39
-
-
Save manikal/00b54787701e389e648d to your computer and use it in GitHub Desktop.
CIImage from CAShapeLayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func layoutSubviews() | |
{ | |
if(self.shapeLayer == nil && self.path != nil && self.pathOriginationSize != CGSizeZero) | |
{ | |
let scaleFactor = self.scaleFactor(self.pathOriginationSize, targetSize: self.bounds.size) | |
_scaledPath = UIBezierPath(CGPath: self.path.CGPath) | |
_scaledPath.lineWidth = self.path.lineWidth | |
_scaledPath.applyTransform(CGAffineTransformMakeScale(scaleFactor.x, scaleFactor.y)) | |
_scaledPath.lineCapStyle = CGLineCap.Round | |
self.shapeLayer = CAShapeLayer() | |
self.shapeLayer.path = _scaledPath.CGPath | |
self.shapeLayer.strokeColor = UIColor.blackColor().CGColor | |
self.shapeLayer.lineWidth = self.path.lineWidth | |
let imageSize = CGPathGetBoundingBox(_scaledPath.CGPath).size | |
UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.mainScreen().scale) | |
let context = UIGraphicsGetCurrentContext() | |
self.shapeLayer.renderInContext(context!) | |
_ = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
let cgImage = self.shapeLayer.contents as! CGImageRef | |
let inputImage = CIImage(CGImage: cgImage) | |
let blurFilter = CIFilter(name: "CIZoomBlur") | |
blurFilter?.setValue(inputImage, forKey: kCIInputImageKey) | |
blurFilter?.setDefaults() | |
self.shapeLayer.filters = [blurFilter!] | |
self.shapeLayer.shouldRasterize = true | |
self.layer.addSublayer(self.shapeLayer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment