【问题标题】:CGContextTranslate rendering wrong locationCGContextTranslate 渲染位置错误
【发布时间】:2014-01-22 03:21:24
【问题描述】:

我正在尝试从 CGContext 创建 UIImageView,但由于某种原因,图像在我的 iPad 版本(但不是 iPhone!)上略微偏离了目标。 我正在传递 textview 本身(也尝试传递 UIView 层......)。 我已经包含了 viewImage 的图片。显然,我希望图像由整个正方形组成,并完美地封闭“在此处查看您的翻转文本(完成后)”......

有什么想法吗?!

这是我的代码:

-(UIImage *)getImage:(UITextView *)v
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions([v frame].size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext([v frame].size);
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, -v.frame.origin.x, -v.frame.origin.y);
    CGContextSaveGState(context);

    [[self.view layer] renderInContext:context];


    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}

这是从上下文渲染的 UIImage(帧原点的 x,y 坐标看起来不错....)

视图控制器有问题吗?我不认为我的设置异常...

这就是我希望返回 UIImage 的样子(或多或少):

【问题讨论】:

    标签: ios7 uiimage cgcontext


    【解决方案1】:

    似乎您的视图 v 嵌入在另一个视图中——在这种情况下,您正在从错误的层拍摄快照。 2个选项: 1. 将要从中获取快照的图层的视图更改为 v 的超级视图(父视图)。 2.将视图v移到外面,使其父视图为self.view。

    干杯!

    【讨论】:

    • 我在另一个子视图中有视图!!!我仍然不确定为什么坐标与超级视图无关……但它现在可以工作了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多