I am working to add support for wide color photos in iOS 10. When the user takes a photo from the camera, I need to use the new API that supports the new color space to save the photo data - UIGraphicsImageRenderer
's jpegData
instead of UIImageJPEGRepresentation
.
I'm running into some troubles with image orientations. Taking a photo on my iPad in portrait, the image isn't being drawn correctly. See the comments below:
Old API:
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 1)
New 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
})
What's the correct way to replace UIImageJPEGRepresentation
with UIGraphicsImageRenderer
's jpegData
?