Skip to content

Instantly share code, notes, and snippets.

@bgayman
Created November 3, 2016 23:18
Show Gist options
  • Save bgayman/e02d148f0549b9cafb6ca11cffed0f8e to your computer and use it in GitHub Desktop.
Save bgayman/e02d148f0549b9cafb6ca11cffed0f8e to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
extension UIView
{
var snapshot: UIImage?
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
let brushWidth: CGFloat = 100
let imageView = UIImageView(image: UIImage(named: "image.jpg"))
let blurEffect = UIBlurEffect(style: .dark)
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.frame = imageView.bounds
let view = UIView(frame: imageView.bounds)
view.addSubview(imageView)
view.addSubview(effectView)
let shapeLayer = CAShapeLayer()
func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint)
{
let path: UIBezierPath
if let layerPath = shapeLayer.path
{
path = UIBezierPath(cgPath: layerPath)
}
else
{
path = UIBezierPath()
}
path.move(to: fromPoint)
path.addLine(to: toPoint)
shapeLayer.path = path.cgPath
shapeLayer.lineWidth = brushWidth
shapeLayer.lineCap = "round"
shapeLayer.strokeColor = UIColor.black.cgColor
}
let image = view.snapshot!
effectView.removeFromSuperview()
let topImageView = UIImageView(image: image)
view.addSubview(topImageView)
topImageView.layer.mask = shapeLayer
drawLine(from: CGPoint(x: 0, y: 0), to: CGPoint(x: imageView.frame.maxX, y: imageView.frame.maxY))
drawLine(from: CGPoint(x: 0, y: imageView.frame.maxY), to: CGPoint(x: imageView.frame.maxX, y: 0))
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment