这是我在我的应用程序中拍摄部分屏幕截图的代码!屏幕截图大小正是我想要的,但是当我在相机胶卷中将其拉起时,图像看起来失焦,而不是我想要的质量。 我写的代码有什么问题?
@IBAction func screenShotButton(sender: Any Object) {
let top: CGFloat = 196
let bottom: CGFloat = 197
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)
}
使用 UIGraphicsBeginImageContextWithOptions
并传递 0 作为刻度。
根据UIGraphicsBeginImageContext
的文档:
此函数等效于调用 UIGraphicsBeginImageContextWithOptions 函数,将不透明参数设置为 NO,比例因子为 1.0。
UIGraphicsBeginImageContextWithOptions
的比例因子参数如下:
要应用于位图的比例因子。如果指定值 0.0,则比例因子将设置为设备主屏幕的比例因子。
换句话说,在iPhone 6等视网膜设备上,原生比例因子为2,iPhone 6 Plus为3。 传递 0 作为刻度参数将为当前设备选择正确的比例因子。