7

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

Palash Sharma
  • 662
  • 1
  • 10
  • 18

3 Answers3

13

Try taking the screenshot this way:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Paul Abrudan
  • 68
  • 1
  • 7
pteofil
  • 4,133
  • 17
  • 27
6

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!

RainCast
  • 4,134
  • 7
  • 33
  • 47
0

Objective C

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
    UIGraphicsBeginImageContext(self.view.window.bounds.size);
}
[self.view.window drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
Pan Mluvčí
  • 1,242
  • 2
  • 21
  • 42