将CGPoint从CGRect转换为其他CGRect [英] Convert CGPoint from CGRect to other CGRect
问题描述
我在视图上放置了一个标签,视图的框架为 CGRectMake(0,0,270,203).现在,我必须使用 CGRectMake(0,0,800,600)来拍摄屏幕快照.因此,我必须将标签从旧矩形转换为新矩形.
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.
谢谢
推荐答案
您可以执行以下操作:
-(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
};
}
这篇关于将CGPoint从CGRect转换为其他CGRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!