将UIView渲染为大于屏幕上的UIImage

时间:2013-03-06 14:49:33

标签: ios uiview uiimage core-graphics

我想将UIView渲染成UIImage。但是,我想渲染它比它在屏幕上更大,没有像素化。

目前我正在使用......

UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[self.pageViewController.view.layer renderInContext:context];
renderedViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我试过......

self.pageViewController.view.transform = CGAffineTransformMakeScale(4.0f, 4.0f);

这会更改视图的大小,但在缩放视图之前会渲染原始大小。

我也试过......

CGContextScaleCTM(UIGraphicsGetCurrentContext(), 4.0, 4.0);

但这只会缩小低分辨率图像,从而导致质量差的结果。

设置......

UIView contentScaleFactor

也没有帮助我渲染任何更大的UIView。

1 个答案:

答案 0 :(得分:0)

尝试这样,如果设备支持视网膜,您将获得2倍大小的视图图像

  - (UIImage *)imageOfView:(UIView *)view
{

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    } else {
        UIGraphicsBeginImageContext(view.bounds.size);
    }

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}
相关问题