当前位置:  开发笔记 > IOS > 正文

用UIGraphicsImageRenderer的jpegData替换UIImageJPEGRepresentation

如何解决《用UIGraphicsImageRenderer的jpegData替换UIImageJPEGRepresentation》经验,为你挑选了0个好方法。

我正在努力当用户需要从相机中的照片添加宽彩色照片在iOS的10支持,我需要使用支持新的色彩空间来保存照片数据的新API - UIGraphicsImageRendererjpegData替代UIImageJPEGRepresentation.

我遇到了一些图像方向的麻烦.在我的iPad上纵向拍照,图像绘制不正确.请参阅以下评论:

旧API:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 1)

新API:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let cgImage = image.cgImage!
let ciImage = CIImage(cgImage: cgImage)

let format = UIGraphicsImageRendererFormat()
format.scale = 1
format.prefersExtendedRange = true

let renderer = UIGraphicsImageRenderer(bounds: ciImage.extent, format: format)
let imageData = renderer.jpegData(withCompressionQuality: 1, actions: { context in
    context.cgContext.draw(cgImage, in: ciImage.extent) //draws flipped horizontally
    //image.draw(at: .zero) //draws rotated 90 degrees leaving black at bottom
    //image.draw(in: ciImage.extent) //draws rotated 90 degrees stretching and compressing the image to fill the rect 
 })

UIImageJPEGRepresentationUIGraphicsImageRenderer's 替换的正确方法是jpegData什么?

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有