【问题标题】:Uiimage from UIView: higher than on-screen resolution?来自 UIView 的 Uiimage:高于屏幕分辨率?
【发布时间】:2011-03-24 00:08:24
【问题描述】:

我有一个 UIView,我通过典型的 UIGraphicsBeginImageContextWithOptions 方法渲染到 UIImage,使用比例为 2.0,因此图像输出将始终是屏幕上显示的“视网膜显示”版本,无论用户的实际屏幕分辨率。

我正在渲染的 UIView 包含图像和文本(UIImages 和 UILabels)。图像以其全分辨率出现在渲染的 UIImage 中,看起来很棒。但 UILabel 似乎已经以 1.0 的比例光栅化,然后放大到 2.0,导致文本模糊。

是我做错了什么,还是有什么方法可以让文本在更高的比例级别上呈现出漂亮和清晰的效果?或者除了使用 UIGraphicsBeginImageContextWithOptions 的缩放参数之外还有其他方法可以得到更好的结果吗?谢谢!

【问题讨论】:

    标签: iphone ios uiview uiimage quartz-graphics


    【解决方案1】:

    解决方案是在绘制之前将标签的 contentsScale 更改为 2,然后立即将其设置回来。我刚刚编写了一个项目来验证它,它在普通视网膜手机(模拟器)中制作 2x 图像时工作得很好。 [如果你有公共场所,我可以告诉我。]

    编辑:扩展代码遍历子视图和任何容器 UIViews 以设置/取消设置比例

    - (IBAction)snapShot:(id)sender
    {
        [self changeScaleforView:snapView scale:2];
    
        UIGraphicsBeginImageContextWithOptions(snapView.bounds.size, snapView.opaque, 2);
        [snapView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        imageDisplay.image = img; // contentsScale
        imageDisplay.contentMode = UIViewContentModeScaleAspectFit;
    
        [self changeScaleforView:snapView scale:1];
    }
    
    - (void)changeScaleforView:(UIView *)aView scale:(CGFloat)scale
    {
        [aView.subviews enumerateObjectsUsingBlock:^void(UIView *v, NSUInteger idx, BOOL *stop)
            {
                if([v isKindOfClass:[UILabel class]]) {
                    v.layer.contentsScale = scale;
                } else
                if([v isKindOfClass:[UIImageView class]]) {
                    // labels and images
                    // v.layer.contentsScale = scale; won't work
    
                    // if the image is not "@2x", you could subclass UIImageView and set the name of the @2x
                    // on it as a property, then here you would set this imageNamed as the image, then undo it later
                } else
                if([v isMemberOfClass:[UIView class]]) {
                    // container view
                    [self changeScaleforView:v scale:scale];
                }       
            } ];
    }
    

    【讨论】:

    • 这行得通,但是如果您尝试捕获 UITableView,则单独更改每个图层会很痛苦。难道没有办法递归地为所有子视图更改它吗?
    • 嗯,我知道没有一个命令,但请注意扩展代码将递归并完成大部分工作。如果您不将 imageViews 设置为 2x 图像,您将不得不做一些特别的事情(这对于非视网膜显示器工作正常,只是在渲染时为系统增加了一些工作。
    【解决方案2】:

    尝试渲染成双倍大小的图像,然后创建缩放图像:

    UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
    // Do stuff
    UImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    newImage=[UIImage imageWithCGImage:[newImage CGImage] scale:2.0 orientation:UIImageOrientationUp];
    

    在哪里: 大小 = realSize * 比例;

    【讨论】:

    • 不知道我明白你在说什么。我没有正在使用的 CGImage,我有一个 UIView,然后我将其渲染到 UIImage...
    • 已编辑。它应该做你想做的事。
    • 这不是要扩大一切吗?那么现在文本图像都会模糊?
    • 试试看。您以双倍分辨率渲染大纹理,然后创建缩放版本。
    • 文本和其他一些 UI 元素在此模式下仍然模糊。
    【解决方案3】:

    在 textview 到 PDF 渲染的上下文中,我一直在努力解决许多相同的奇怪问题。我发现组成视图的 CALayer 对象上有一些记录在案的属性。也许设置相关(子)层的 rasterizationScale 会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2020-02-25
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多