I am calling AVFoundation
's delegate method to handle a photo capture, but I am having difficulty converting the AVCapturePhoto
it generates into an UIImage
with the correct orientation. Although the routine below is successful, I always get a right-oriented UIImage
(UIImage.imageOrientation
= 3). I have no way of providing an orientation when using the UIImage(data: image)
and attempting to first use photo.cgImageRepresentation()?.takeRetainedValue()
also doesn't help. Please assist.
Image orientation is critical here as the resulting image is being fed to a Vision Framework workflow.
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// capture image finished
print("Image captured.")
if let imageData = photo.fileDataRepresentation() {
if let uiImage = UIImage(data: imageData){
// do stuff to UIImage
}
}
}
UPDATE 1: Reading Apple's Photo Capture Programming Guide (out of date for iOS11), I did manage to find one thing I was doing wrong:
- On every capture call (
self.capturePhotoOutput.capturePhoto
) one must setup a connection with thePhotoOutput
object and update its orientation to match the device's orientation at the moment the picture is taken. For doing that, I created an extension ofUIDeviceOrientation
and used it on thesnapPhoto()
function I created to call the capture routine and wait for thedidFinishProcessingPhoto
delegate method to be executed. I've added a snapshot of the code because the code sample delimiters here don't seem to be displaying them correctly.
Update 2 Link to full project on GitHub: https://github.com/agu3rra/Out-Loud