UIImage截屏失去了质量。

3
我正在合并两张图片,然后使用以下代码截取屏幕截图:
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_AddText=viewImage;

[dragView removeFromSuperview];
imgV_SelectedImg.image=nil;
imgV_SelectedImg.image=img_AddText;
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

问题是最终图像失去质量时会模糊。
5个回答

9

尝试使用带有选项的UIGraphicsBeginImageContext版本

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

2
我用这段代码获取到了屏幕截图,画质很好,并且位置也很准确。
-(UIImage *)takeScreenShot
{
    CGRect grabRect;
    grabRect = CGRectMake(0,70,320,260);
    UIGraphicsBeginImageContextWithOptions(grabRect.size, self.view.opaque, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
    [self.view.layer renderInContext:ctx];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}

它给我提供了出色的快照..

(注:已保留HTML标签,无需修改)

1

UIGraphicsBeginImageContextWithOptions(size, NO, 2.0); 通过将比例从1.0增加到2.0,解决了我的问题。


传递0以使用设备分辨率- 未来证明自己并保持兼容性。 - Adam Waite

1
我在 UIImage 类上创建了一个类别,可能会对您有所帮助。 它是这样的:
+ (UIImage*)imageWithView:(UIView *)view opaque:(BOOL)opaque bgColor:(UIColor*)bgColor{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, [[UIScreen mainScreen] scale]);

    if(!opaque){
        [bgColor set];
    }
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

对我来说它运行良好,没有检测到模糊。尝试使用它。如果您仍然遇到问题,那么问题很可能在于您的保存代码...

干杯... :)


1
你的图像不会失去质量的原因是你使用了 UIGraphicsBeginImageContextWithOptions(size, opaque, scale),就像 Dcritelli答案 一样。 - David Rönnqvist

0
你是否为Retina显示器提供了图像? 你应该检查一下。 你可能在模拟器中运行(使用retina)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接