如何在swift中截取UIView的截图?

Sam*_*ain 22 screenshot uiview ios swift

我有一个名为overView的UIView:

overView.frame = CGRectMake(self.view.frame.width/25, self.view.frame.height/25, self.view.frame.width/1.3, self.view.frame.height/1.2)
Run Code Online (Sandbox Code Playgroud)

我想仅截取此视图的屏幕截图,而不是整个屏幕.并制作尺寸截图:

 (CGSizeMake(2480,3508 )
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(2480,3508 ), false, 0);
self.view.drawViewHierarchyInRect(CGRectMake(-self.view.frame.width/25, -self.view.frame.height/25,2480,3508), afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)

屏幕截图具有所需的大小,但它需要整个视图的屏幕截图,而不仅仅是"overView".

Jir*_*cak 38

要绘制一个视图,只需使用:

    // Begin context
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)

    // Draw view in that context
    drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)

    // And finally, get image
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)

如果你想多次使用它,可能扩展会完成这项工作:

// Swift4

extension UIView {

    func takeScreenshot() -> UIImage {

        // Begin context
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)

        // Draw view in that context
        drawHierarchy(in: self.bounds, afterScreenUpdates: true)

        // And finally, get image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if (image != nil)
        {
            return image!
        }
        return UIImage()
    }
}
Run Code Online (Sandbox Code Playgroud)

//老斯威夫特

extension UIView {

    func takeScreenshot() -> UIImage {

        // Begin context
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale)

        // Draw view in that context
        drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)

        // And finally, get image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}
Run Code Online (Sandbox Code Playgroud)

要解释这些参数的作用:

UIGraphicsBeginImageContextWithOptions()创建一个临时渲染上下文,其中绘制原始.第一个参数size是缩放图像的目标大小.第二个参数isOpaque用于确定是否呈现alpha通道.对于没有透明度的图像(即Alpha通道),将此设置为false可能会导致图像呈现粉红色调.第三个参数比例是显示比例因子.设置为0.0时,使用主屏幕的比例因子,Retina显示的比例因子为2.0或更高(iPhone 6 Plus上为3.0).

有关它的更多信息,请访问http://nshipster.com/image-resizing/

至于绘制调用,Apple Docs 在此处此处详细说明了它


Ale*_*dro 14

swift 4和iOS 10+

extension UIView {

  func screenshot() -> UIImage {
    return UIGraphicsImageRenderer(size: bounds.size).image { _ in
      drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
    }
  }

}
Run Code Online (Sandbox Code Playgroud)


Áng*_*lez 11

Alessandro's answer的另一种选择,更简洁和 Swift 风格:

extension UIView {

    var snapshot: UIImage {
        return UIGraphicsImageRenderer(size: bounds.size).image { _ in
            drawHierarchy(in: bounds, afterScreenUpdates: true)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)