4

I have placed one label on view and view's frame is CGRectMake(0,0,270,203). Now I have to take screen shot of view with CGRectMake(0,0,800,600). So I have to convert label from old rect to new rect.

here is code which I used to take screen shot:

CGRect rect = [view bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Here is the code which I used to convert point:

CGRect f = [view convertRect:lblMessage.frame fromView:viewMessage];

But I am not able to get actual position from label in new image. Can anyone help where I am wrong.

Here I have attached image for more clarification. In small view, I have add one label and I have to convert label's frame as per big image view.

Convert label from this view to big view.

enter image description here

Thanks,

Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
  • Is your label actually inside the `view`? Please add some details about the view hierarchy. Please, specify where you expect the label to be on the resulting image. – Sulthan Jun 13 '14 at 20:45

2 Answers2

0
    +(UIImage*)screenShotOf:(UIView*)view atScale:(CGFloat)scale
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];

        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return img;
    }

Here You need to adjust the scale property as you needed.

0

You could do something like this:

-(CGPoint) convertPoint: (CGPoint) point fromRect: (CGRect) fromRect toRect: (CGRect) toRect {
    return (CGPoint){
        (toRect.size.width/fromRect.size.width) * point.x,
        (toRect.size.height/fromRect.size.height) * point.y
    };
}
ddiego
  • 3,247
  • 1
  • 28
  • 18