Created
April 3, 2019 13:35
-
-
Save iSapozhnik/013e76b76ce43febdb6eec70cddb7b3f to your computer and use it in GitHub Desktop.
Screenshot and Blur
This file contains hidden or 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
private func screenshot() -> UIImage? { | |
let format = UIGraphicsImageRendererFormat() | |
format.scale = UIScreen.main.scale | |
format.opaque = false | |
let renderer = UIGraphicsImageRenderer(bounds: layer.bounds, format: format) | |
return renderer.image { _ in | |
self.drawHierarchy(in: bounds, afterScreenUpdates: false) | |
} | |
} | |
private func blur(_ image: UIImage?) -> UIImage? { | |
guard let image = image, let ciImage = CIImage(image: image) else { return nil } | |
let blurFilter = CIFilter(name: "CIGaussianBlur") | |
blurFilter?.setValue(ciImage, forKey: kCIInputImageKey) | |
blurFilter?.setValue(20, forKey: kCIInputRadiusKey) | |
guard let outputCIImage = blurFilter?.outputImage else { return nil } | |
let croppedImage = outputCIImage.cropped(to: ciImage.extent) | |
return UIImage(ciImage: croppedImage) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment