4

I want to take a screenshot from my app programmatically and the code working fine but I have a UIVisualEffectView with blur effect and the screenshot gave me the image without blur! How I can make the screenshot take the blur also?

UIGraphicsBeginImageContextWithOptions(fullView.bounds.size, true, 1)
self.fullView.layer.renderInContext(UIGraphicsGetCurrentContext())
var viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)
BandarAljahmi
  • 77
  • 2
  • 4

3 Answers3

4

To retain the UIVisualEffectView Blur when taking an image of the screen programmatically, you must be taking an image of the entire screen.

Here is a more technical definition provided by Apple:

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. - Apple Documentation

user3721428
  • 298
  • 1
  • 4
  • 14
  • Thank you for your answer, is there any other way to add the blur effect to part of the screen when I take the screenshot even without the UIVisualEffectView? – BandarAljahmi Nov 25 '14 at 04:41
  • 1
    @BandarAljahmi I added an example of how to blur an image programmatically, though this should be the accepted answer. – Jasper Blues Nov 25 '14 at 06:14
2

After taking a screenshot, a blur effect can be added programmatically. Here's an example of using the GPUImage library, which can be installed using CocoaPods. A similar effect can also be achieved using native Apple libraries.

GPUImagePicture *picture = [[GPUImagePicture alloc] initWithImage:snapShot];
GPUImageiOSBlurFilter *blurFilter = [[GPUImageiOSBlurFilter alloc] init];
[blurFilter setBlurRadiusInPixels:4];

[picture addTarget:blurFilter];
[blurFilter useNextFrameForImageCapture];
[picture processImage];

UIImage *processed = [blurFilter imageFromCurrentFramebuffer];
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
1

I had this same problem. I had a view controller with a visual effect (blur) applied to a background image and some other non-blurred view on top and I wanted to take a screenshot of that controller.

As highlighted by @user3721428 trying to capture the view doesn't work. For my surprise trying to render the window layer in context doesn't work.

What worked is to get a snapshotView from UIScreen: let view = UIScreen.main.snapshotView(afterScreenUpdates: true)

And then grab an image from that view using a method like in @neave answer here

gabbo
  • 461
  • 5
  • 9