ios - 如何快速截取 UIView 的屏幕截图?

标签 ios swift uiview screenshot

我有一个名为 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)

我只想截取此 View 的屏幕截图,而不是我的整个屏幕。并制作尺寸截图:

 (CGSizeMake(2480,3508 )

这是我的代码:

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

屏幕截图具有所需的大小,但它会截取整个 View 的屏幕截图,而不仅仅是“overView”。

最佳答案

要绘制一个 View ,只需使用这个:

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

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

//swift 4

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

//老 swift

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
    }
}

解释这些参数的作用:

UIGraphicsBeginImageContextWithOptions() creates a temporary rendering context into which the original is drawn. The first argument, size, is the target size of the scaled image. The second argument, isOpaque is used to determine whether an alpha channel is rendered. Setting this to false for images without transparency (i.e. an alpha channel) may result in an image with a pink hue. The third argument scale is the display scale factor. When set to 0.0, the scale factor of the main screen is used, which for Retina displays is 2.0 or higher (3.0 on the iPhone 6 Plus).

更多信息请点击此处 http://nshipster.com/image-resizing/

至于绘制调用,Apple Docs 对其进行了详细解释 herehere

关于ios - 如何快速截取 UIView 的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582222/

相关文章:

ios - 为什么 NSArray 的对象在 ARC 模式下不调用 dealloc 方法?

ios - 为什么 SpriteKit 中的 Sprite 不会从墙上反弹

ios - Swift 中 UIViewcontroller 的实例

swift - 在 Swift 中捕获 Objective-C 异常

iOS在UIView中打洞不是正方形

ios - 是否可以结束 SKAction Action 中途?

ios - 在 Swift 中创建一个应用程序生命周期对象(不可能被释放)

ios - UITableView 最初不加载数据

带有按钮的 iOS subview 不响应触摸。

swift - 以编程方式使用约束的自定义 UIView