0

I added a UIImageView on an ImageView, so i want to take screenshot of these two imageviews so that they look like one, any other recommendation is also appreciated.

I made a button that helped me take a screen shot, I have added x-axis and y axis,

func takeScreenshot(_ shouldSave: Bool = true) -> UIImage {
    var screenshotImage :UIImage?
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 20, height: 104), false, scale);
    guard let context = UIGraphicsGetCurrentContext() else {return screenshotImage ?? UIImage(imageLiteralResourceName: "loading")}
    layer.render(in:context)
    screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if let image = screenshotImage, shouldSave {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
    return screenshotImage ?? UIImage(named: "loading")!
}

I expect that the screenshot taken, takes the screenshot of the imageview. Screenshot is attached,enter image description here

valosip
  • 3,167
  • 1
  • 14
  • 26
iahmerr
  • 17
  • 10

2 Answers2

0

I believe you're trying to say

You have 2 UIImageViews with different Images and then you want to take a screenshot of those 2 UIImageViews to make 1 image.

If that's the case, the easiest way to do solve it is to wrap those 2 UIImageViews inside a UIView. Then convert that UIView into UIImage. So you can have the Screenshot of those 2 UIImageViews in one.

In case you need code to convert UIView to UIImage:

extension UIView {
    func toImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

I'm not sure if this the solution to your problem.

Ly Boung
  • 185
  • 9
0

Put those two image views inside a UIView, constraint them all properly. Then take a screenshot of that UIView. Follow this for screenshot: How to take screenshot of a UIView in swift?

Mumtaz Hussain
  • 925
  • 9
  • 23