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.
Thanks,
+(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.
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
};
}
来源:https://stackoverflow.com/questions/23465725/convert-cgpoint-from-cgrect-to-other-cgrect