我正在尝试使用以下方法从当前 ARFrame
获取图像:
if let imageBuffer = sceneView.session.currentFrame?.capturedImage {
let orientation = UIApplication.shared.statusBarOrientation
let viewportSize = sceneView.bounds.size
let transformation = sceneView.session.currentFrame?.displayTransform(for: orientation, viewportSize: viewportSize)
let ciImage = CIImage(cvPixelBuffer: imageBuffer).transformed(by: transformation)
}
对于风景,效果很好。对于人像,我以错误的角度获取图像(旋转 180 度)。知道为什么吗?
最佳答案
At first I should say that it definitely is an unpleasant bug.
问题是,当您转换 Portrait
图像,ARFrame 包含的内容,对于 CIImage 或 CGImage,它失去了方向并将其逆时针旋转 180 度。此问题仅影响 Portrait
图片。 Landscape
完全不受影响。
发生这种情况是因为 Portrait
图像在转换阶段没有关于其方向的信息,因此,即使转换为 CIImage 或 CGImage,纵向图像仍保持纵向模式。
要解决此问题,您应该比较“标准”景观的 width
/height
带有“非标准”肖像的 width
/height
,如果这些值不同,则将图像旋转到 CW 180 度(或应用方向案例 .portraitUpsideDown)。
希望这对您有所帮助。
关于swift - displayTransform 调用后图像方向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903747/