How to save a layered image using a extension function

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
    }

@timswift Do you still have issues with this?

hi @timswift,
I have not run your code, but from what I see at first glance, you are finishing the context, an then starting another context.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.