将CGPoint从一个CGRect转换到另一个CGRect。

4

我在视图上放置了一个标签,视图的框架是CGRectMake(0,0,270,203)。现在我需要使用CGRectMake(0,0,800,600)来截取视图的屏幕截图。因此,我需要将标签从旧的矩形转换为新的矩形

以下是我用于截取屏幕截图的代码:

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

这是我用来转换点的代码:

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

但我无法从新图像的标签中获取实际位置。有人可以帮忙看看我哪里错了吗?
这里我附上了更多说明的图片。 在小视图中,我添加了一个标签,并且我必须将标签的框架转换为大图像视图。
谢谢。

你的标签实际上在 view 里面吗?请添加一些关于视图层次结构的细节。请指定您期望标签在生成的图像中的位置。 - Sulthan
2个回答

0
你可以像这样做:
-(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
    };
}

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;
    }

在这里,您需要根据需要调整比例属性。


我已经使用了你的代码。但是标签与小视图中的标签不在同一帧中。 - Nirmalsinh Rathod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接