3

I have the following for converting a UIView to UIImage

+(UIImage*) createUIImageFromView:(UIView *)view frame:(CGRect)frame;
{
    if (UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(frame.size, NO, 2.0f);
    }
    else
    {
        UIGraphicsBeginImageContext(view.frame.size);
    }

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

However, I can only select the size of the image but not where in the UIView should be converted to the image. For example, if I want to convert the rectangle CGRect(10,10,20,20) only but not the entire UIView, how do I do it?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
ABCD
  • 7,914
  • 9
  • 54
  • 90
  • I think you can crop image after you have got it in whole. You can do it with ease using CGImage functions. – bealex Nov 19 '11 at 05:10

1 Answers1

4

You can set the coordinate transformation matrix on your context. This allows you to change the coordinate system, so when the layer draws at (0,0), it will actually render somewhere else in the context. The functions you want to use to actually modify the CTM are documented in the Transforming User Space section of the CGContext docs.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347