在 swift 中尝试以编程方式截取屏幕截图时 UIBlurEffect 不起作用

UIBlurEffect not working when trying to take screenshot programically in swift

我以编程方式模糊了我的视图,并添加了带有屏幕截图的共享扩展,但屏幕截图并没有模糊我在图像中的视图。 swift

中的所有节目

模糊代码

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

截图代码

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

谢谢

试试这样截图:

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

我刚刚发现 Apple 实际上已经记录了如何拍摄 UIVisualEffectView 的快照:

正在捕获 UIVisualEffectView 的快照 许多效果需要来自承载 UIVisualEffectView 的 window 的支持。尝试仅拍摄 UIVisualEffectView 的快照将导致不包含效果的快照。 要拍摄包含 UIVisualEffectView 的视图层次结构的快照,您必须拍摄包含它的整个 UIWindow 或 UIScreen 的快照。

谢谢!

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;