I want to detect ball and have AR model interact with it. I used opencv for ball detection and send center of ball which I can use in hitTest
to get coordinates in sceneView
. I have been converting CVPixelBuffer
to UIImage
using following function:
static func convertToUIImage(buffer: CVPixelBuffer) -> UIImage?{
let ciImage = CIImage(cvPixelBuffer: buffer)
let temporaryContext = CIContext(options: nil)
if let temporaryImage = temporaryContext.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer)))
{
let capturedImage = UIImage(cgImage: temporaryImage)
return capturedImage
}
return nil
}
This gave me rotated image:
Then i found about changing orientation using:
let capturedImage = UIImage(cgImage: temporaryImage, scale: 1.0, orientation: .right)
While it gave correct orientation while device is in portrait, rotating device to landscape again gave rotated image.
Now I am thinking about handling it using viewWillTransition
. But before that i want to know:
- If there is other way around to convert image with correct orientation?
- Why does this happen?