使用UIGraphicsGetImageFromCurrentImageContext的Swift屏幕快照图像分辨率很差

时间:2019-06-30 07:12:04

标签: ios swift

我的日历屏幕滚动很长,试图共享完整日历的完整屏幕快照,使用了此扩展程序,但在iPhone 8和真实设备上的图像质量为202x1024,显示效果很差。如何获得包括屏幕滚动区域在内的全分辨率屏幕截图。

fileprivate extension UITableView {
    func screenshot() -> UIImage? {
        let savedContentOffset = contentOffset
        let savedFrame = frame

        UIGraphicsBeginImageContext(contentSize)
        contentOffset = .zero
        frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)

        guard let context = UIGraphicsGetCurrentContext() else { return nil }

        layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        contentOffset = savedContentOffset
        frame = savedFrame

        return image
    }
}

1 个答案:

答案 0 :(得分:1)

您是否尝试过为图像上下文指定比例?

UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)