我正在努力当用户需要从相机中的照片添加宽彩色照片在iOS的10支持,我需要使用支持新的色彩空间来保存照片数据的新API - UIGraphicsImageRenderer
的jpegData
替代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 })
UIImageJPEGRepresentation
用UIGraphicsImageRenderer
's 替换的正确方法是jpegData
什么?