Search code examples
objective-cuiviewuiimageviewretina-display

Save uiview as specified size


I have an uiimageview that is 600x600 and a uitexview loaded in a 200x200 uiview.

I would like to export this uiview to 600x600, same as the uiimageview size.

However If I use the code below I can only generate a size of 400x400, that is the retina size of the uiview.

Is there any code I can achieve this?

UIGraphicsBeginImageContextWithOptions(outputView.bounds.size, outputView.opaque, 2);
[outputView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Solution

  • Have you tried changing the bounds of the view to what you need?

    outputView.bounds = CGRectMake(0,0,600,600);
    UIGraphicsBeginImageContextWithOptions(outputView.bounds.size, outputView.opaque, 2);     [outputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    outputView.bound = ... original bounds here ...
    

    If the internal view is set to be resized automatically, this should work seamlessly. Otherwise, you could change the frame of the internal view as well.