部分屏幕截图和图像质量下降 [英] Partial screenshot & loss of image quality

查看:268
本文介绍了部分屏幕截图和图像质量下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打印相机胶卷,电子邮件,短信,FB,Twitter等的部分屏幕截图...已选择部分屏幕-顶部100像素,底部100像素.

I'm printing a partial screenshot for camera roll, email, sms, FB, Twitter, etc... Partial screen selected - 100 pixels from top, 100 from bottom.

我使用了以下代码:

let top: CGFloat = 100
let bottom: CGFloat = 100

let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

UIGraphicsBeginImageContext(size)

let context = UIGraphicsGetCurrentContext()!

CGContextTranslateCTM(context, 0, -top)

view.layer.renderInContext(context)

let snapshot = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil)

生成的屏幕截图质量很差.

The resultant screenshot was of a poor quality.

我研究了几个小时,发现几个人有类似的问题.我不太想修改针对我的问题的解决方案.

I have researched for hours and found several people have a similar problem. I can't quite get my head around modifying solutions given to them to my issue.

我确实设法找到了一个半解决方案.我改变了:

I did manage to find a semi-fix. I changed:

UIGraphicsBeginImageContext(size)

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,true,2.0)

这实际上将我的屏幕截图放大了2.0倍

which essentially scales up my screenshot by factor of 2.0

尽管图像比我预期的要大,这似乎可以为我提供更清晰/更好质量的部分屏幕截图.

This seems to give me a sharper/better quality partial screenshot, although the image is larger than I intended.

还有其他我可以应用的解决方案吗?

Is there another solution I can apply that might be more appropriate?

谢谢!

推荐答案

问题是UIGraphicsBeginImageContext()创建的图像上下文的缩放比例为1.0,但是您的图层(因为它正在屏幕上显示)具有比例等于设备屏幕的比例-最有可能高于1.0.

The problem is that UIGraphicsBeginImageContext() creates an image context with a scale of 1.0, however your layer (as it's being displayed on-screen) has a scale equivalent to the device's screen's scale – which is most likely higher than 1.0.

因此,您希望按照正确的说明使用UIGraphicsBeginImageContextWithOptions(),但不要通过2.0作为比例尺(它将在2x显示器上起作用,而不能在其他显示器上使用),而是通过0.0.

You therefore want to be using UIGraphicsBeginImageContextWithOptions() as you correctly said, but instead of passing 2.0 for the scale (which will work on 2x displays, but not on others), pass 0.0.

作为文档对scale参数说:

应用于位图的比例因子. 如果您指定值为0.0,则比例因子将设置为设备主屏幕的比例因子.

因此,上下文将在您执行此操作时自动检测屏幕的比例因子,并且在您运行该设备的任何设备上图像都将清晰清晰显示.

The context will therefore automatically detect the scale factor of the screen when you do this, and the image will come out nice and clear on any device you run it on.

这篇关于部分屏幕截图和图像质量下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆