# How to save a layered image using a extension function

**URL:** <https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884>\
**Category:** Swift\
**Created:** [October 13, 2019, 11:52pm UTC](https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884 "2019-10-13T23:52:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [October 13, 2019, 11:52pm UTC](https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884/1 "2019-10-13T23:52:36Z")

</div>

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
    }

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 14, 2019, 3:07pm UTC](https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884/2 "2019-11-14T15:07:54Z")

</div>

@timswift Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [November 15, 2019, 10:12am UTC](https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884/3 "2019-11-15T10:12:35Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 28, 2020, 3:52pm UTC](https://forums.kodeco.com/t/how-to-save-a-layered-image-using-a-extension-function/84884/4 "2020-03-28T15:52:39Z")

</div>

Closed on March 28, 2020, 3:52pm UTC

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