Screenshot programmatically and blur in Swift

prevent screenshot ios swift
screenshieldkit

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)

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];

ios - Screenshot programmatically and blur in Swift, After taking a screenshot, a blur effect can be added programmatically. Here's an example of using the GPUImage library, which can be  Screenshot programmatically and blur in Swift. ios,xcode,swift,screenshot,blur. 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.

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

Three ways of adding Gaussian blur to your Swift mobile app's , Learn how to add Gaussian blur to your mobile app written in Swift! need to take a screenshot of the first one and pass it to another controller. Screenshot programmatically and blur in Swift. ios,xcode,swift,screenshot,blur. 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.

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

How to add blur and vibrancy using UIVisualEffectView, As of iOS 8.0, visual effects such as blur and vibrancy are a cinch because Apple provides a built in UIView subclass that does all the hard  I want to prevent taking screenshot of a page in app. how to do it programmatically so that screenshots cannot be taken. Found code to detect screenshot. Can it be deleted as soon as a screenshot

snapshotViewAfterScreenUpdates:, Returns a snapshot view based on the contents of the current view. SDKs. iOS 7.0+; Mac Catalyst 13.0+; tvOS 9.0+ If you want to apply a graphical effect, such as blur, to a snapshot, use the drawViewHierarchyInRect:afterScreenUpdates:  r/swift: Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts

UIVisualEffectView, iOS 8.0+; Mac Catalyst 13.0+; tvOS 9.0+ Attempting to take a snapshot of only the UIVisualEffectView will result in a snapshot that does not contain the effect. 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)

How to take a screeshot in iOS programmatically, In iOS we can press the HOME and the POWER button together at any time and a screenshot of what's currently being displayed will be saved  How do I take a full screen Screenshot in Swift? Programmatically Screenshot Screenshot in Swift iOS? Taking screenshot from part of screen in swift How to detect when the user takes a screenshot

Comments
  • 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 I added an example of how to blur an image programmatically, though this should be the accepted answer.