我正试图模仿appWillResignActive上的AVCaptureVideoPreviewLayer,就像在iOS相机应用程序中一样。
由于光栅化 AVCaptureVideoPreviewLayer 并且模糊本身会产生一个空框架,因此我通过 didOutputSampleBuffer 和 applicationWillResignActive保持 CIImage 来接近它,我采取 CIImage - >将 CIFilter CIGaussianBlur 应用于它,将 UIImageView 添加到 AVCaptureVideoPreviewLayer 并制作 UIImageView' s 图像是我应用 CIGaussianBlur 的模糊的 UIImage 版本。
到目前为止,这似乎工作正常......但是,它似乎产生了相当多的条带,在高斯模糊中显示质量问题。
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!)
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let width = CVPixelBufferGetWidth(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmap = CGBitmapInfo(rawValue: CGBitmapInfo.ByteOrder32Little.rawValue|CGImageAlphaInfo.PremultipliedFirst.rawValue)
let context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, bitmap.rawValue)
let quartzImage = CGBitmapContextCreateImage(context!)
CVPixelBufferUnlockBaseAddress(imageBuffer!,CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
self.cameraBufferImage = CIImage(CGImage: quartzImage!)
}
func blurPreviewWindow() {
let previewLayer = self.previewView.layer as? AVCaptureVideoPreviewLayer
previewLayer?.connection.enabled = false
let context = CIContext(options: nil)
let inputImage = self.cameraBufferImage!.imageByApplyingOrientation(6)
let clampFilter = CIFilter(name: "CIAffineClamp")
clampFilter!.setDefaults()
clampFilter!.setValue(inputImage, forKey: "inputImage")
if let currentFilter = CIFilter(name: "CIGaussianBlur") {
currentFilter.setValue(clampFilter!.outputImage, forKey: "inputImage")
currentFilter.setValue(50.0, forKey: "inputRadius")
if let output = currentFilter.outputImage {
if let cgimg = context.createCGImage(output, fromRect: inputImage.extent) {
let processedImage = UIImage(CGImage: cgimg)
self.previewBlurImageView = UIImageView(frame: self.previewView.bounds)
self.previewBlurImageView?.alpha = 0
self.previewView.addSubview(self.previewBlurImageView!)
self.previewBlurImageView?.image = processedImage
self.previewBlurImageView?.contentMode = .ScaleToFill
self.previewBlurImageView?.layer.filters = [currentFilter]
UIView.animateWithDuration(0.2, delay: 0.0, options: [.BeginFromCurrentState], animations: {
() -> Void in
self.previewBlurImageView?.alpha = 1
}, completion: { _ in })
}
}
}
}
对于这个iOS 10时代,可能会有一个完全不同的方法吗?
这可能是一个色彩空间问题吗?由于测试设备是iPhone 7,它有很宽的颜色?
答案 0 :(得分:3)
是CIContext。
更改:
let context = CIContext(options: nil)
要:
let context = CIContext(options: [kCIContextWorkingColorSpace: CGColorSpaceCreateDeviceRGB(),
kCIContextOutputColorSpace: CGColorSpaceCreateDeviceRGB(),
kCIContextUseSoftwareRenderer: false])
没有更多的条纹或文物!!!