# I would like to add 2 UIImageViews into a single UIImageView. 2 UIImageView nested into a larger UIImageView. Here is a picture of what I am looking for

**URL:** <https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247>\
**Category:** Swift\
**Created:** [December 19, 2016, 1:58am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247 "2016-12-19T01:58:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [December 19, 2016, 1:58am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247/1 "2016-12-19T01:58:47Z")

</div>

This is the link to my stack overflow question [http://stackoverflow.com/questions/41135781/how-to-merge-2-uiimageview-into-another-uiimageview-swift-3](http://stackoverflow.com/questions/41135781/how-to-merge-2-uiimageview-into-another-uiimageview-swift-3)

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [December 19, 2016, 4:33am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247/2 "2016-12-19T04:33:20Z")

</div>

![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/5/4/5472916d77a363ae1cb58baf77733477c3d92c8f.png)

OK so I see from the picture that you want to have one UIImageView as a parent, with two other UIImageViews added to it as child views?

If Storyboard editor this doesn’t work. To me it seems like a bug but you cannot drag something into a UIImageView, even though it inherits from UIView and should work the same way - it does in code. A way around it is if you create a UIView that is the size you want the parent UIImageView to be; then you add three UIImageViews to it. Call them A, B and C as in your diagram.

You position and size C so that it is the full width and height of the background UIView. Probably the best constraints to use are leading/trailing, top/bottom with constants of 0 so that it covers the whole UIView and will stay that way if it is resized. Then you size and position A and B the way you want them - the only thing you need to be worried about is that A and B are both in front of C, or else they will still be there but will be behind C.

Alternatively you could add the UIImageView C in Storyboard, then add A and B as subviews of it programmatically.

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [December 19, 2016, 5:19am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247/3 "2016-12-19T05:19:38Z")

</div>

I know how to set up the storyboards but how can I save the photo so that all 3 uiimageviews are nested onto each other. So that the parent view is in the background of the image of the 2 child views. parent view c; child view a and b

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [December 19, 2016, 5:32am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247/4 "2016-12-19T05:32:59Z")

</div>

OK then - here’s some code to use. It renders a view into an image context. You can see I am saving it to the app’s documents directory but you can do whatever you like with `saveImage` when it returned from `UIGraphicsGetImageFromCurrentImageContext`. The view you should render would either be the parent UIView, or the parent UIImageView, depending on which layout strategy you have chosen.

```
    let contextSize = CGSize(width: xDimension, height: yDimension)
    UIGraphicsBeginImageContext(contextSize);
    if let context = UIGraphicsGetCurrentContext() {
        
        view.layer.render(in: context)
        if let saveImage = UIGraphicsGetImageFromCurrentImageContext() {
            let imageData = UIImageJPEGRepresentation(saveImage, 1.0)
            let fileManager = FileManager.default
            let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
            let documentDirectory = urls.first
            do {
                let fileURL = documentDirectory!.appendingPathComponent("\(title).jpg")
                print(fileURL)
                try imageData?.write(to: fileURL)
            } catch {
                print("error")
            }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![elliott](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elliott/32/52293_2.png) [@elliott](https://forums.kodeco.com/u/elliott)\
**Post date:** [December 22, 2016, 3:34am UTC](https://forums.kodeco.com/t/i-would-like-to-add-2-uiimageviews-into-a-single-uiimageview-2-uiimageview-nested-into-a-larger-uiimageview-here-is-a-picture-of-what-i-am-looking-for/24247/5 "2016-12-22T03:34:32Z")

</div>

Just to add to this, the func UIGraphicsBeginImageContext(\_ size: CGSize) will render the context with a scale factor of 1.0.

You may refer to use:  
UIGraphicsBeginImageContextWithOptions(contextSize, true, 0.0);  
(This will render the context at the scale factor that matches the device’s main screen)
