Convert CGPoint from CGRect to other CGRect

懵懂的女人 提交于 2019-12-04 03:32:51

问题


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,


回答1:


    +(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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!