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

Dan*_*anM 19 iphone quartz-graphics uiview uiimage ios

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

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

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

Dav*_*d H 9

解决方案是在绘制之前将标签的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];
            }       
        } ];
}
Run Code Online (Sandbox Code Playgroud)