captureOutput中的方向问题:didOutputSampleBuffer:来自连接:

时间:2017-04-10 21:23:48

标签: ios swift uiimageview uiimage avcapturesession

我正在尝试处理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
    }
}

这就是我在预览中看到的内容:

UIImageView preview

非常感谢提前

1 个答案:

答案 0 :(得分:1)

我找到了!事实证明我必须在该方法中添加行connection.videoOrientation = AVCaptureVideoOrientation.portrait ...