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

Ios captureOutput:didOutputSampleBuffer:来自连接中的方向问题:,ios,swift,uiimageview,uiimage,avcapturesession,Ios,Swift,Uiimageview,Uiimage,Avcapturesession,我正在尝试处理AVCaptureSession的每一帧,并在UIImageView中预览过滤后的图像。它可以工作,但UIImageView中的图像会出现旋转(和扭曲)。我一直试图在这里和谷歌找到答案,但我找不到任何有效的解决方案。。。有人知道该尝试什么吗 顺便说一句,我使用的是Swift 3.0 这是我正在使用的代码: func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer:

我正在尝试处理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.patrait
检查你的图像方向
UIImageOrientation。对
不应该是
UIImageOrientation.up
原始方向…谢谢。我试过了,但结果是一样的。。。