2017-04-10 155 views
1

我正在尝试处理AVCaptureSession的每个帧,并预览UIImageView中的过滤图像。它工作但UIImageView中的图像出现旋转(和扭曲)。我一直在试图找到答案在这里和谷歌,但我找不到任何工作解决方案...有没有人有什么想法试试?captureOutput方向问题:didOutputSampleBuffer:来自连接:

顺便说一句,我使用的雨燕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

许多在此先感谢

+0

检查你的图像方向'UIImageOrientation.right'不应该是'UIImageOrientation.up'为原始方向... –

+0

感谢这个。我尝试过,但结果是一样的... –

回答

0

我发现它!原来我必须在该方法中添加行connection.videoOrientation = AVCaptureVideoOrientation.portrait ...