app4g
app4g

Reputation: 840

Resize UIView prior to taking a screenshot

I'm trying to convert an UIView to an UIImage to be saved to the photo album.

Thus far, I have been able to get this, but the resultant image is based on the UIView's layout on screen. (eg: Portrait or Landscape)

My goal is to have the same output (eg: Square) no matter if the UIView is in portrait or in landscape.

This is the current code that I'm using (I tried a lot of differing methods)

extension UIView {
    func image() -> UIImage {
        let imageRenderer = UIGraphicsImageRenderer(bounds: bounds)
        if let format = imageRenderer.format as? UIGraphicsImageRendererFormat {
          format.opaque = true
        }
        let image = imageRenderer.image { context in
            return layer.render(in: context.cgContext)
        }
        return image
    }
}

I am able to change the UIImage dimension to the desired size, but the UIView still follows what its originally layout is on screen.

  let f = ergTrainChartView.frame;      //Grab current
  ergTrainChartView.frame = CGRect(x: f.origin.x, y: f.origin.y, width: 365, height: 365)

enter image description here

My goal is to have the left image no matter if the device is in Portrait or Landscape (iPad or iPhone) mode.

Thanks.

Upvotes: 2

Views: 234

Answers (0)

Related Questions