如何拍摄UIView部分的截图?

Chr*_*ris 10 screenshot ios swift wkwebview

我希望用户在Swift中以编程方式按下按钮后继续我的应用程序并获取应用程序的屏幕截图.我知道这UIGraphicsGetImageFromCurrentImageContext()是截图,但我不想要整个屏幕的图片.我希望弹出一个矩形(有点像裁剪工具),用户可以拖动矩形并调整其大小,只截取屏幕的某个部分.我希望矩形覆盖WKWebView并裁剪网页视图的图片.

Rob*_*Rob 30

标准快照技术是drawHierarchy(in:afterScreenUpdates:)将其绘制到图像上下文中.在iOS 10及更高版本中,您可以使用UIGraphicsImageRenderer:

extension UIView {

    /// Create image snapshot of view.
    ///
    /// - Parameters:
    ///   - rect: The coordinates (in the view's own coordinate space) to be captured. If omitted, the entire `bounds` will be captured.
    ///   - afterScreenUpdates: A Boolean value that indicates whether the snapshot should be rendered after recent changes have been incorporated. Specify the value false if you want to render a snapshot in the view hierarchy’s current state, which might not include recent changes. Defaults to `true`.
    ///
    /// - Returns: The `UIImage` snapshot.

    func snapshot(of rect: CGRect? = nil, afterScreenUpdates: Bool = true) -> UIImage {
        return UIGraphicsImageRenderer(bounds: rect ?? bounds).image { _ in
            drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你会像这样使用它:

let image = webView.snapshot(of: rect)
Run Code Online (Sandbox Code Playgroud)

在iOS 10之前,您将获得图像的一部分,您可以使用CGImage方法cropping(to:).例如:

extension UIView {

    /// Create snapshot
    ///
    /// - Parameters:
    ///   - rect: The coordinates (in the view's own coordinate space) to be captured. If omitted, the entire `bounds` will be captured.
    ///   - afterScreenUpdates: A Boolean value that indicates whether the snapshot should be rendered after recent changes have been incorporated. Specify the value false if you want to render a snapshot in the view hierarchy’s current state, which might not include recent changes. Defaults to `true`.
    ///
    /// - Returns: Returns `UIImage` of the specified portion of the view.

    func snapshot(of rect: CGRect? = nil, afterScreenUpdates: Bool = true) -> UIImage? {
        // snapshot entire view

        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
        drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
        let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        // if no `rect` provided, return image of whole view

        guard let image = wholeImage, let rect = rect else { return wholeImage }

        // otherwise, grab specified `rect` of image

        guard let cgImage = image.cgImage?.cropping(to: rect * image.scale) else { return nil }
        return UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
    }

}
Run Code Online (Sandbox Code Playgroud)

使用这个方便的操作员:

extension CGRect {
    static func * (lhs: CGRect, rhs: CGFloat) -> CGRect {
        return CGRect(x: lhs.minX * rhs, y: lhs.minY * rhs, width: lhs.width * rhs, height: lhs.height * rhs)
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用它,您可以:

if let image = webView.snapshot(of: rect) {
    // do something with `image` here
}
Run Code Online (Sandbox Code Playgroud)

对于Swift 2的演绎,请参阅此答案的上一版本.