调整使用UIGraphicsBeginImageContextWithOptions拍摄的屏幕截图

时间:2015-02-23 13:33:33

标签: ios swift uiimage image-resizing

在我正在开发的应用中,客户输入一些个人信息并在屏幕上签名。然后我以编程方式截取完整视图并将其转换为base64。

这很好,但是,由于图像的大小,它需要大约15秒才能将其转换为base64并将其发送到API服务器。完整质量的图像尺寸与iPad Air分辨率(1536x2048)相同。

作为PNG保存在服务器上后,图像的重量大约为410kb。

我希望将图像捕捉的大小调整为768x1024(它的一半)而不会失去清晰度。我认为这样可以节省时间和存储空间。

我目前正在拍摄"截图"使用以下功能:

func screenCaptureAndGo() {
    //Create the UIImage
    UIGraphicsBeginImageContextWithOptions(view.frame.size, view.opaque, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    imageData = UIImagePNGRepresentation(image)
    imageDataString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
}

我遗漏了API服务器的东西,因为它并不真正相关。

1 个答案:

答案 0 :(得分:0)

我最后只使用了Scale属性。它没有第一次工作,这就是为什么我没有想到再次使用它,但是在玩弄它之后我就开始工作了。

我使用0.7作为比例量,图像现在以100kb计时,转换并发送base64字符串需要4秒。

相关问题