Skip to content

Instantly share code, notes, and snippets.

@iSapozhnik
Created April 3, 2019 13:35
Show Gist options
  • Save iSapozhnik/013e76b76ce43febdb6eec70cddb7b3f to your computer and use it in GitHub Desktop.
Save iSapozhnik/013e76b76ce43febdb6eec70cddb7b3f to your computer and use it in GitHub Desktop.
Screenshot and Blur
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