Uiview 如何使用扩展功能保存分层图像

Uiview 如何使用扩展功能保存分层图像,uiview,uiimage,extension-methods,calayer,uigraphicscontext,Uiview,Uiimage,Extension Methods,Calayer,Uigraphicscontext,我正在使用扩展函数将uiview保存为uiimage。代码用于保存uiimage。然而,我试图做的是在保存到照片库的图像上保存一个透明图像。因此,我尝试使用扩展函数保存分层图像。现在只有uiivew正在保存,第二层没有保存 class ViewController: UIViewController,UINavigationControllerDelegate { @IBAction func press(_ sender: Any) { let jake = d

我正在使用扩展函数将uiview保存为uiimage。代码用于保存uiimage。然而,我试图做的是在保存到照片库的图像上保存一个透明图像。因此,我尝试使用扩展函数保存分层图像。现在只有uiivew正在保存,第二层没有保存

    class ViewController: UIViewController,UINavigationControllerDelegate {
    @IBAction func press(_ sender: Any) {
        let jake = drawingView.takeSnapshotOfView(view: drawingView)
              guard let selectedImage = jake else {
                              print("Image not found!")
                              return
                 }
                 UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }}

  func takeSnapshotOfView(view:UIView) -> UIImage? {
            UIGraphicsBeginImageContext(CGSize(width: view.frame.size.width, height: view.frame.size.height))
            view.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height), afterScreenUpdates: true)


            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

                let star:UIImage = UIImage(named: "e.png")!
                            let newSize = CGSize(width: star.size.width, height: star.size.height  )

                            UIGraphicsBeginImageContextWithOptions(newSize, false, star.scale)

                            star.draw(in: CGRect(x: newSize.width/12,
                                                 y: newSize.height/8,
                                                 width: newSize.width/1.2,
                                                 height: newSize.height/1.2),
                                      blendMode:CGBlendMode.normal, alpha:1)




                            UIGraphicsEndImageContext()

            return image
        }

这是一个UIView扩展,它接受CGRect和UIImage,您也可以为它提供另一个CGRect或CGSize,使其在水印位置/大小方面更具动态性

extension UIView {        
    /// Takes a screenshot of a UIView, with an option to clip to view bounds and place a waterwark image 
    /// - Parameter rect: offset and size of the screenshot to take
    /// - Parameter clipToBounds: Bool to check where self.bounds and rect intersect and adjust size so there is no empty space
    /// - Parameter watermark: UIImage of the watermark to place on top
    func screenshot(for rect: CGRect, clipToBounds: Bool = true, with watermark: UIImage? = nil) -> UIImage {
        var imageRect = rect
        if clipToBounds {
            imageRect = bounds.intersection(rect)
        }
        return UIGraphicsImageRenderer(bounds: imageRect).image { _ in
        drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
        watermark?.draw(in: CGRect(origin: imageRect.origin, size: CGSize(width: 32, height: 32))) // update origin to place watermark where you want, with this update it will place it in top left or screenshot.
        }
    }
}
你可以这样称呼它:

let image = self.view.screenshot(for: CGRect(x: 0, y: 0, width: 200, height: 200), with: UIImage(named: "star"))
这将适用于调用屏幕截图(…)的视图的所有子视图


对于使用上述扩展名的用户,我在此添加了其他信息

是视图的透明图像部分,还是在保存图像时添加?有点像水印?对于任何使用上述扩展的人,我在这里添加了额外的信息