Last active
April 11, 2023 23:28
-
-
Save sammcode/48571172db2e17e4fb6b763098464428 to your computer and use it in GitHub Desktop.
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
// Modified version of the extension found in https://github.com/FlexMonkey/Blurable | |
extension UIView { | |
func asImage() -> UIImage { | |
let format = UIGraphicsImageRendererFormat() | |
format.scale = 1.0 | |
let renderer = UIGraphicsImageRenderer(size: bounds.size, format: format) | |
return renderer.image { ctx in | |
layer.render(in: ctx.cgContext) | |
} | |
} | |
func blur(blurRadius: CGFloat) { | |
guard let blur = CIFilter(name: "CIGaussianBlur") else { return } | |
let image = self.blurViewImage | |
blur.setValue(CIImage(image: image), forKey: kCIInputImageKey) | |
blur.setValue(blurRadius, forKey: kCIInputRadiusKey) | |
let ciContext = CIImage.ciContext | |
guard let result = blur.value(forKey: kCIOutputImageKey) as? CIImage else { return } | |
let boundingRect = CGRect(x: 0, y: 0, width: frame.width, height: frame.height) | |
guard let cgImage = ciContext.createCGImage(result, from: boundingRect) else { return} | |
let filteredImage = UIImage(cgImage: cgImage) | |
let blurOverlay = BlurOverlay() | |
blurOverlay.backgroundColor = .clear | |
blurOverlay.frame = boundingRect | |
blurOverlay.image = filteredImage | |
blurOverlay.contentMode = .scaleAspectFill | |
blurOverlay.frame.origin = frame.origin | |
UIView.transition(from: self, to: blurOverlay, duration: 0, options: UIView.AnimationOptions.curveEaseIn, completion: nil) | |
objc_setAssociatedObject(self, &BlurableKey.blurable, blurOverlay, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | |
} | |
func removeBlur() { | |
guard let blurOverlay = objc_getAssociatedObject(self, &BlurableKey.blurable) as? BlurOverlay else { return } | |
self.frame.origin = blurOverlay.frame.origin | |
UIView.transition(from: blurOverlay, to: self, duration: 0.2, options: UIView.AnimationOptions.curveEaseIn, completion: nil) | |
objc_setAssociatedObject(self, &BlurableKey.blurable, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | |
} | |
func removeBlurAndDeallocateBlurImage() { | |
removeBlur() | |
objc_setAssociatedObject(self, &BlurableKey.blurViewImage, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | |
} | |
var isBlurred: Bool { | |
return objc_getAssociatedObject(self, &BlurableKey.blurable) is BlurOverlay | |
} | |
var blurViewImage: UIImage { | |
get { | |
if let image = objc_getAssociatedObject(self, &BlurableKey.blurViewImage) as? UIImage { | |
return image | |
} else { | |
let image = self.asImage() | |
objc_setAssociatedObject(self, &BlurableKey.blurViewImage, image, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | |
return image | |
} | |
} | |
} | |
} | |
class BlurOverlay: UIImageView {} | |
struct BlurableKey { | |
static var blurable = "blurable" | |
static var blurViewImage = "blurViewImage" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment