【问题标题】:Convert CGPoint from CGRect to other CGRect将 CGPoint 从 CGRect 转换为其他 CGRect
【发布时间】:2014-05-05 05:56:14
【问题描述】:

我在视图上放置了一个标签,视图的框架是 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里面吗?请添加有关视图层次结构的一些详细信息。请指定您希望标签在结果​​图像上的位置。

标签: ios objective-c cgrect cgpoint


【解决方案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;
    }

这里需要根据需要调整 scale 属性。

【讨论】:

  • 我使用了你的代码。但是标签与小视图中的框架不同。
【解决方案2】:

你可以这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2015-07-07
    • 2014-07-29
    • 2016-03-15
    • 1970-01-01
    相关资源
    最近更新 更多