6

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

模糊代码

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()

谢谢

4

3 回答 3

12

尝试以这种方式截取屏幕截图:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
于 2015-05-14T13:37:29.990 回答
6

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

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

谢谢!

于 2016-11-02T02:13:38.783 回答
0

目标 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;
于 2020-04-03T10:09:23.323 回答