【问题标题】:Screenshot Only Part of Screen - Swift屏幕截图仅部分屏幕 - Swift
【发布时间】:2015-03-14 14:02:53
【问题描述】:

我正在使用此 Swift 代码截取我的应用程序的屏幕截图:

UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

我将如何只截取屏幕的一部分而不是全部截屏,就像我在这里所做的那样?

【问题讨论】:

    标签: ios swift screenshot fullscreen


    【解决方案1】:

    例如,如果您想在位置(50,50) 高度为(100,150) 处截取UIView 矩形的屏幕截图。

    首先将 Image Context 设置为矩形的大小。这将设置图像大小。

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,150), false, 0);
    

    现在在此上下文中绘制视图,屏幕截图的可见部分(50,50)

    开始
    self.view.drawViewHierarchyInRect(CGRectMake(-50,-50,view.bounds.size.width,view.bounds.size.height) afterScreenUpdates: true)
    

    这将截取(100,150) 的屏幕截图,因为我们设置了context 的大小。现在从中获取 UIImage。

    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    

    【讨论】:

    • 感谢您的快速答复!
    • mama nuvvu keka mama ;)
    • 不适合我。你介意看看我的代码吗? stackoverflow.com/questions/34887164/…
    • 对于 swift 5 使用 CGRect 而不是 CGRectmake 即 .. CGRect(x: -50,y: -50,width: view.bounds.size.width,height: view.bounds.size.height )
    • 谁能回答为什么将 x 和 y 的原点都乘以 -1?不应该只有y轴吗?
    【解决方案2】:

    我认为这是拍摄部分屏幕的最佳答案:

    func screenShot() {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width*0.99,self.frame.size.height*0.70), false, 0)
        var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
        self.view?.drawViewHierarchyInRect(CGRectMake(-01, -01, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true)
        var screenShot  = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    

    【讨论】:

      【解决方案3】:

      用于按另一个 UIView 的区域选择快照

      // viewObject: UIView linked from storyboard
      var bounds= CGRect(x: -viewObject.frame.minX,y: -viewObject.frame.minY,width: view.bounds.size.width,height: view.bounds.size.height)
      
      UIGraphicsBeginImageContext(viewObject.frame.size)
              view.drawHierarchy(in: bounds, afterScreenUpdates: true)
              let outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
              UIGraphicsEndImageContext()
      

      【讨论】:

      • 你能解释为什么否定是必要的吗?
      【解决方案4】:
      func captureView(view: UIView) -> UIImage {
          var screenRect: CGRect = view.bounds
          UIGraphicsBeginImageContext(screenRect.size)
          UIGraphicsBeginImageContextWithOptions(screenRect.size, true, 0)
          var ctx: CGContext = UIGraphicsGetCurrentContext()!
          UIColor.black.set()
          ctx.fill(screenRect)
          view.layer.render(in: ctx)
          var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
          UIGraphicsEndImageContext()
          return newImage
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 2021-07-12
        相关资源
        最近更新 更多