如何从UIView捕获高分辨率图像 [英] How to capture high resolution image from UIView

查看:59
本文介绍了如何从UIView捕获高分辨率图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望大家都安全.

我知道这个问题曾问过几次,但没有得到一个完美的答案.

I know this question asked several times but not getting a perfect answer.

我只是想以高分辨率从UIView捕获图像,主要是图像不应该是模糊的.

I just wanted to capture an image from UIView with high resolution the main this is that image should not be a blur.

我已经尝试过此代码

extension UIView {
    func asImage() -> UIImage {
            let renderer = UIGraphicsImageRenderer(size: self.bounds.size)
            let capturedImage = renderer.image {
                (ctx) in
                self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
            }
            return capturedImage
    }
}

现在,当我捕获图像并缩放时,文本变得模糊了.

Right now while I capture the image and zoom the text is blurred.

预先感谢

已编辑我正在尝试从UIView创建高分辨率图像.当我缩放图像的1部分时,文本模糊.

Edited I am trying to create high-resolution image from UIView. When I zoom 1 part of image the text is blurred.

请检查下图

推荐答案

extension UIView {
    func takeScreenshot() -> UIImage? {
        var screenshotImage :UIImage?
        let layer = self.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
        self.drawHierarchy(in: layer.bounds, afterScreenUpdates: true)
        screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return screenshotImage
    }
}

或者您可以缩放视图(或视图的临时副本),然后截图.

Or maybe you can scale your view (or temporary copy of view) and then take a screenshot.

这篇关于如何从UIView捕获高分辨率图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆