8th Grader 8th Grader - 1 month ago 9
iOS Question

UIBlurEffect not working when trying to take screenshot programically in swift

I've blurred my view programmatically and added a share extension with screenshot but the screenshot doesn't blur my view in the image. All programming in swift

Code for blurring

let blurEffect = UIBlurEffect(style:.Dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = imageView2.frame
self.view.addSubview(blurView)


Code for screenshot

UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()


Thanks

Answer

Try taking the screenshot this way:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Comments