【问题标题】:UIBlurEffect not working when trying to take screenshot programically in swift尝试以编程方式快速截取屏幕截图时,UIBlurEffect 不起作用
【发布时间】:2015-05-14 11:25:15
【问题描述】:

我以编程方式模糊了我的视图并添加了一个带有屏幕截图的共享扩展,但屏幕截图并没有模糊我在图像中的视图。 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()

谢谢

【问题讨论】:

    标签: ios uigraphicscontext uiblureffect


    【解决方案1】:

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

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

    【讨论】:

    • 虽然你能帮帮我,但状态栏不见了
    • view.drawViewHierarchy... 更改为 view.window?.drawViewHierarchy...
    • 不知道为什么你的view不会有窗口。试试这个:UIApplication.sharedApplication().delegate?.window.
    • 这非常适合小视图。对于大视图,屏幕截图只是变成白色图像。您知道为什么会发生这种情况以及解决方案吗?
    • 这个方法有点慢。明智地使用它。当你不需要捕获 UIVisualEffectView 时,总是使用snapshotView(afterScreenUpdates: true)
    【解决方案2】:

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

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

    谢谢!

    【讨论】:

      【解决方案3】:

      目标 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多