Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 499
Palash Sharma Avatar asked May 14 '15 11:05

Palash Sharma


2 Answers

Try taking the screenshot this way:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
like image 78
pteofil Avatar answered Sep 28 '22 00:09

pteofil


I just found Apple has actually documented on how to take a snapshot of UIVisualEffectView:

Capturing a Snapshot of a UIVisualEffectView Many effects require support from the window that hosts the UIVisualEffectView. Attempting to take a snapshot of only the UIVisualEffectView will result in a snapshot that does not contain the effect. To take a snapshot of a view hierarchy that contains a UIVisualEffectView, you must take a snapshot of the entire UIWindow or UIScreen that contains it.

Thanks!

like image 27
RainCast Avatar answered Sep 28 '22 00:09

RainCast