在swift中渲染UIImageView时质量很差

时间:2016-04-22 12:36:17

标签: ios swift uiimageview

我在swift中使用下面的代码将UIImageView捕获到一个图像中。它的工作原理但图像的质量与UIImageView上显示的图像质量不同。有没有办法在捕获此图像时配置质量?

private func getScreenshow(imageView:UIImageView) -> UIImage{

    UIGraphicsBeginImageContext(self.imageView.frame.size)
    let context = UIGraphicsGetCurrentContext()

    imageView.layer.renderInContext(context!)
    let screenShot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(screenShot, nil, nil, nil)
    return screenShot
}

2 个答案:

答案 0 :(得分:2)

经过一番搜索,我发现了问题。我使用下面的代码来替换“UIGraphicsBeginImageContext”并且它可以工作。

UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, true, 0)

答案 1 :(得分:2)

这段代码看起来很奇怪(你为什么不使用-A n?)但我不知道你的用例的完整上下文。

正如您所发现的,质量下降的原因是您无视屏幕的视网膜尺度。

阅读UIGraphicsBeginImageContextUIGraphicsBeginImageContextWithOptions的文档,您会看到前者使用'比例因子imageView.image'。

相关问题