繁体   English   中英

来自UIView的Uiimage:高于屏幕分辨率?

[英]Uiimage from UIView: higher than on-screen resolution?

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

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

有没有什么我做错了,或者是否有某种方法可以使文本在更高级别的水平上呈现出美观和清晰? 或者除了使用UIGraphicsBeginImageContextWithOptions的缩放参数之外还有其他方法可以做到这一点吗? 谢谢!

解决方案是在绘制之前将标签的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];
            }       
        } ];
}

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

UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
// Do stuff
UImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

newImage=[UIImage imageWithCGImage:[newImage CGImage] scale:2.0 orientation:UIImageOrientationUp];

其中:size = realSize * scale;

在textview到PDF渲染的背景下,我一直在苦苦挣扎。 我发现在构成视图的CALayer对象上有一些记录的属性。 也许设置相关(子)层的rasterizationScale有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM