以编程方式创建更高分辨率的PNG图像

时间:2013-04-18 20:37:02

标签: ios objective-c uiimage high-resolution

所以我有一些copypasta方法来创建一个PNG文件并且它的工作非常出色,但是,当打印这个PNG时,它看起来有点模糊"像一个低分辨率的图像。有没有办法创建具有更高像素深度的PNG?

这是我目前的代码:

- (UIImage*) renderScrollViewToImage
{
    UIImage* image = nil;

    UIGraphicsBeginImageContext(self.scrollView.contentSize);
    {
        CGPoint savedContentOffset = self.scrollView.contentOffset;
        CGRect savedFrame = self.scrollView.frame;

        self.scrollView.contentOffset = CGPointZero;
        self.scrollView.frame = CGRectMake(0, 0, _scrollView.contentSize.width, _scrollView.contentSize.height);

        [self.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();

        _scrollView.contentOffset = savedContentOffset;
        _scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();

    if (image != nil) {
        return image;
    }
    return nil;
}

1 个答案:

答案 0 :(得分:1)

尝试更换:

UIGraphicsBeginImageContext(self.scrollView.contentSize);

UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, NO, 0.0);

将考虑视网膜缩放。文档:

UIGraphicsBeginImageContextWithOptions
Creates a bitmap-based graphics context with the specified options.

void UIGraphicsBeginImageContextWithOptions(
   CGSize size,
   BOOL opaque,
   CGFloat scale
);
Parameters
size
The size (measured in points) of the new bitmap context. This represents the size of the image returned by the UIGraphicsGetImageFromCurrentImageContext function. To get the size of the bitmap in pixels, you must multiply the width and height values by the value in the scale parameter.
opaque
A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specify YES to ignore the alpha channel and optimize the bitmap’s storage. Specifying NO means that the bitmap must include an alpha channel to handle any partially transparent pixels.
scale
The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.