0

I have a UIView, setup on a superview using auto-layout and its subviews are setup too through auto-layout. I want to be able to resize the view to capture its resized contents to a UIImage, while keeping the view displayed on screen the same size.

I know I can use:

UIGraphicsBeginImageContextWithOptions(myView.bounds.size, NO, 0.0);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();

to obtain the image from the view,

but how do I resize the view JUST for capturing its contents, whilst leaving it untouched on display?

Thanks

EDIT for clarity

I want to be able to increase the width of the view, changing the aspect ratio of the view, allowing for the auto layout constraints to arrange its subviews to its new size.

Is it possible to simply copy the view, don't add it as a subview of the main window (so it's not visible) and change its frame, then capture it?

DotSlashSlash
  • 5,143
  • 5
  • 30
  • 32

1 Answers1

0

I have created a 2nd instance of the view controller and set it up with the same properties as the original, except at a different size. I also had to add the new view as a subview, capture its contents via UIGraphicsGetImageFromCurrentImageContext() and then remove the view from the subview.

This does not seem like a very efficient solution, and it would be interesting to know if there is a better way to do this.

DotSlashSlash
  • 5,143
  • 5
  • 30
  • 32