1

所以我有一些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;
}
4

1 回答 1

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.
于 2013-04-18T20:52:52.453 回答