如何使用扩展功能保存分层图像 [英] How to save a layered image using a extension function

查看:131
本文介绍了如何使用扩展功能保存分层图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扩展功能将uiview保存为uiimage.该代码可保存uiimage.但是,我想做的是在保存到照片库的图像上保存透明图像.因此,我尝试使用扩展功能保存分层图像.现在,只有uiivew被保存,而第二层没有被保存.

I am using a extension function to save a uiview as a uiimage. The code works to save the uiimage. However what I am trying to do is save a transparent image over the image being saved to the photo gallery. So I am trying to save a layered image using a extension function. Right now only the uiivew is being save and the 2nd layer is not being saved.

    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
        }

推荐答案

这里是一个包含CGRect和UIImage的UIView扩展,此外,您还可以为其提供另一个CGRect或CGSize,以使其对水印放置更加动态/大小

Here is a UIView extension that takes in CGRect and UIImage, and alternitavely you can also supply it another CGRect or CGSize to make it more dynamic for the watermark placement/size

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"))

这将适用于调用屏幕快照(...)的视图的所有子视图

This will work for all subviews of the view that calls screenshot(...)

对于使用上述扩展名的任何人,我都在此 answer

For a anyone using the above extension, I added additional info in this answer

这篇关于如何使用扩展功能保存分层图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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