我正在尝试处理 AVCaptureSession 的每一帧并在 UIImageView 中预览过滤后的图像。它可以工作,但 UIImageView 中的图像出现旋转(并且扭曲)。我一直试图在这里和谷歌中寻找答案,但我找不到任何有效的解决方案...有人知道要尝试什么吗?
顺便说一句,我正在使用 Swift 3.0
这是我正在使用的代码:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let attachments = CMCopyDictionaryOfAttachments(kCFAllocatorMalloc, sampleBuffer!, kCMAttachmentMode_ShouldPropagate)
let coreImage = CIImage(cvImageBuffer: pixelBuffer!, options: attachments as! [String : Any]?)
var filteredImage = UIImage(ciImage: coreImage, scale: 1.0, orientation: UIImageOrientation.right)
if filter {
NSLog("FILTER ACTIVATED")
let filterType = CIFilter(name: "CISepiaTone")
filterType?.setValue(coreImage, forKey: kCIInputImageKey)
filterType?.setValue(0.5, forKey: kCIInputIntensityKey)
filteredImage = UIImage(ciImage: filterType!.value(forKey: kCIOutputImageKey) as! CIImage!, scale: filteredImage.scale, orientation: UIImageOrientation.right)
}
DispatchQueue.main.async() {
self.imageViewPreview.image = filteredImage // UIImageView
}
}
这就是我在预览中看到的:
提前非常感谢
我找到了!事实证明我必须在该方法中添加行
connection.videoOrientation = AVCaptureVideoOrientation.portrait
...
对于 iOS 17+:
创建一个rotationCoordinator变量:
private var rotationCoordinator: AVCaptureDevice.RotationCoordinator?
在
configurePreviewLayer()
中分配旋转协调器:
func configurePreviewLayer(_ view: UIView) {
guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video,
position: .back) else { return }
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer?.videoGravity = .resizeAspectFill
guard let previewLayer = previewLayer else { return }
view.layer.addSublayer(previewLayer)
rotationCoordinator = AVCaptureDevice.RotationCoordinator(device: videoDevice, previewLayer: previewLayer)
}
然后用它来将
videoRotationAngle
分配给connection
中的captureOutput()
:
func captureOutput(_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection) {
guard let coordinator = rotationCoordinator else { return }
connection.videoRotationAngle = coordinator.videoRotationAngleForHorizonLevelCapture
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
let ciImage = CIImage(cvImageBuffer: imageBuffer)
let context = CIContext()
guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return }
currentFrame = UIImage(cgImage: cgImage)
}
注意使用
videoRotationAngleForHorizonLevelCapture
而不是 videoRotationAngleForHorizonLevelPreview
。