This is the link to my stack overflow question http://stackoverflow.com/questions/41135781/how-to-merge-2-uiimageview-into-another-uiimageview-swift-3
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.
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
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")
}
}
}
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)