ios - 如何使用UIBezierpath获取用于绘图的View(Drawing View)的截图

标签 ios swift xcode layer uibezierpath

我有一个位于 ScrollView 上的绘图 View 。绘图完成后,我需要一张我将上传到服务器的绘图的屏幕截图。

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

let path = UIBezierPath()

     for i in self.drawView.path{

            path.append(i)

        }

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

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

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

enter image description here

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

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)

仅供引用,我尝试使用此矩形并创建一个 View (带有边框层的清晰颜色)并将其放置在绘图上,它工作得很好但是当我 try catch 图像时它超出了范围 enter image description here

这是我用来截屏的函数

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,但是当我再次尝试使用提供 View 边界的函数将 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)

enter image description here

最佳答案

对 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(您已通过编程方式)

关于ios - 如何使用UIBezierpath获取用于绘图的View(Drawing View)的截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568634/

相关文章:

ios - 在 Swift 中使用 RestKit

ios - 在变量 iOS 中存储完成处理程序( objective-c )

ios - 我的应用占用了 iPad 存储上的虚拟空间

swift - 一个socket连接占用两个tcp端口正常吗?

ios - cocoa pod 的所有源文件都编译了两次

ios - 如何在工作区中的两个 xcode 项目之间共享文件?

ios - flutter 错误 : No profiles for ‘xxx’ were found: Xcode couldn’t find any iOS App Development provisioning profiles matching ‘xxx’

ios - 如何遍历动态大小的数组并将属性作为参数传递给可变参数函数?

ios - UIApplication.sharedApplication.open() 不加载 url

ios - RxAlamofire - 下载完成事件丢失