【问题标题】:How to get a screenshot of the View (Drawing View) used for drawing using UIBezeir path如何使用 UIBezierpath 获取用于绘图的视图(绘图视图)的屏幕截图
【发布时间】:2018-03-30 05:01:32
【问题描述】:

我有一个在滚动视图上的绘图视图。绘图完成后,我需要将要上传到服务器的绘图屏幕截图。

我正在使用 UIBezeir 路径在视图上绘制。

let path = UIBezierPath()

     for i in self.drawView.path{

            path.append(i)

        }

self.drawView.path 是一个 NSArray,包含绘图的所有 bezeir 路径。

但是当我使用这条路径的边界框并获取坐标的最大值和最小值并尝试捕获屏幕截图时,我得到了这个

var rect:CGRect = CGRect(x: path.bounds.minX, y: path.bounds.minY, width: path.bounds.maxX, height: path.bounds.maxY)

我也尝试给出路径本身的边界

let rect:CGRect = CGRect(x: path.bounds.origin.x - 5, y: path.bounds.origin.y - 5, width: path.bounds.size.width + 5, height: path.bounds.size.height + 5)

仅供参考,我尝试使用此矩形并创建一个视图(带有边框层的清晰颜色)并将其放置在绘图上,它工作得很好但是当我尝试捕获图像时它超出了边界

这是我用来截屏的函数

func imgScreenShot(bounds:CGRect) -> UIImage{

        let rect: CGRect = bounds
        self.drawView.isOpaque = false
        self.drawView.backgroundColor = UIColor.clear
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        var context: CGContext? = UIGraphicsGetCurrentContext()
        if let aContext = context {
            self.drawView.layer.render(in: aContext)
        }
        var capturedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //let finalImage = scaleImage(image: capturedImage)

        return capturedImage!


    }

我也尝试使用此功能获取 UIView

let vw = self.drawView.resizableSnapshotView(from: rect, afterScreenUpdates: true, withCapInsets: UIEdgeInsets.zero)

这给了我一个完美的 UIView 与绘图,但是当我再次尝试使用提供视图边界的函数将 UIView 转换为 UIImage 时,我得到一个空白图像。

任何人都可以提出我做错了什么或任何其他解决方案来解决这个问题,图像的边界正好从绘图的边界开始

let vw = self.drawView.resizableSnapshotView(from: rect2, afterScreenUpdates: true, withCapInsets: UIEdgeInsets.zero)
            vw?.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
            vw?.layer.borderColor = UIColor.red.cgColor
            vw?.layer.borderWidth = 1
            self.drawView.addSubview(vw!)
            let image = vw?.snapshotImage
            let imgView = UIImageView(frame: CGRect(x: 250, y: 50, width: 100, height: 100))
            imgView.layer.borderColor = UIColor.gray.cgColor
            imgView.layer.borderWidth = 1
            self.drawView.addSubview(imgView)

【问题讨论】:

    标签: ios swift xcode layer uibezierpath


    【解决方案1】:

    对 UIView 和 UIImage 进行扩展,因此在整个应用程序生命周期中,您可以使用这些方法(我将在下面描述)进行捕获任何特定 UIView 的屏幕截图并调整现有图像的大小(如果需要)。

    这是 UIView扩展:-

    extension UIView {
    
        var snapshotImage : UIImage? {
    
            var snapShotImage:UIImage?
    
            UIGraphicsBeginImageContext(self.frame.size)
    
            if let context = UIGraphicsGetCurrentContext() {
    
                self.layer.render(in: context)
    
                if let image = UIGraphicsGetImageFromCurrentImageContext() {
                    UIGraphicsEndImageContext()
                    snapShotImage = image
                }
            }
            return snapShotImage
        }
    }
    

    这是 UIImage扩展:-

    extension UIImage {
    
        func resizeImage(newSize:CGSize) -> UIImage? {
    
            var newImage:UIImage?
    
            let horizontalRatio = newSize.width / size.width
            let verticalRatio = newSize.height / size.height
    
            let ratio = max(horizontalRatio, verticalRatio)
    
            let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
    
            UIGraphicsBeginImageContext(newSize)
    
            if let _ = UIGraphicsGetCurrentContext() {
    
                draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
    
                if let image = UIGraphicsGetImageFromCurrentImageContext() {
    
                    UIGraphicsEndImageContext()
                    newImage = image
                }
            }
    
            return newImage
        }
    }
    

    如何在我们想要的类中使用这些函数?

    if let snapImage = yourUIView.snapshotImage {
    
                ///... snapImage is the desired image you want and its dataType is `UIImage`.
    
               ///... Now resize the snapImage into desired size by using this one 
    
             if let resizableImage = snapImage.resizeImage(newSize: CGSize(width: 150.0, height: 150.0)) {
    
    
                print(resizableImage)
            }
    
            }
    

    这里 yourUIView 是指,你用来绘制一些输入的那个。它可以是 IBOutlet 以及您的 UIView(您已以编程方式

    【讨论】:

    • 非常感谢您的输入,尝试扩展,将更新您的输出。
    • imgur.com/a/7pWi7。仍然无法正常工作,请查看我在此处发布的两张图片,这是我的代码。请检查我编辑的问题我根据您的建议添加了我尝试过的代码
    • 好的,你会尽快更新吗?我写的代码是完全正确的,也可以工作。但我认为当你要调整视图大小时,那里出了点问题。可能规模会更大,这就是你得到空白图像的原因。
    • @SritejaC ,可以先获取 snapShort Image 然后再调整大小吗?
    • 我只需要一张任意大小/比例的图片,但其边界与我的问题的最后一张图片完全一致。
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多