ios - Swift iOS 实时对视频应用滤镜?

标签 ios swift avfoundation calayer core-image

我正在尝试实时对视频应用滤镜,但是当我点击按钮来转换 CALayer 时,没有应用任何更改;这很好奇,因为过滤器已通过但在屏幕上不可见。这是我用来呈现 AVOutput 的 UIView 的子类...

//AVCamPreview Class
import Foundation
import UIKit
import AVFoundation


class AVCamPreviewView: UIView {


var session: AVCaptureSession? {
    get {

        return (self.layer as! AVCaptureVideoPreviewLayer).session
    }
    set (session) {

        (self.layer as! AVCaptureVideoPreviewLayer).session = session
    }
}

override class func layerClass() -> AnyClass {


    return AVCaptureVideoPreviewLayer.self
}
 }




  //CameraViewController
 //Properties

   lazy var context: CIContext = {
    let eaglContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
    let options = [kCIContextWorkingColorSpace : NSNull()]
    return CIContext(EAGLContext: eaglContext, options: options)
}()
lazy var filterNames: [String] = {
    return ["#nofilter","CIPhotoEffectNoir","CIPhotoEffectProcess"]
}()

//Apply Filter func
 @IBAction func applyFilter(_sender: UIButton) {
    let filterName = filterNames[_sender.tag]
    filter = CIFilter(name: filterName)
    self.filterButtonsContainer.center.x =  self.filterButtonsContainer.center.x - self.view.frame.width
}

 // MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
func captureOutput(captureOutput: AVCaptureOutput!,didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,fromConnection connection: AVCaptureConnection!) {
    autoreleasepool {
        let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

        let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)!
        self.currentVideoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
        self.currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

        var outputImage = CIImage(CVPixelBuffer: imageBuffer)

        if self.filter != nil {

            self.filter.setValue(outputImage, forKey: kCIInputImageKey)
            outputImage = self.filter.outputImage!


        }


        if self.isWriting {
            if self.assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData == true {
                var newPixelBuffer: CVPixelBuffer? = nil

                CVPixelBufferPoolCreatePixelBuffer(nil, self.assetWriterPixelBufferInput!.pixelBufferPool!, &newPixelBuffer)

                self.context.render(outputImage, toCVPixelBuffer: newPixelBuffer!, bounds: outputImage.extent, colorSpace: nil)

                let success = self.assetWriterPixelBufferInput?.appendPixelBuffer(newPixelBuffer!, withPresentationTime: self.currentSampleTime!)

                if success == false {
                    print("Pixel Buffer failed")
                }
            }
        }

        let orientation = UIDevice.currentDevice().orientation
        var t: CGAffineTransform!
        if orientation == UIDeviceOrientation.Portrait {
            t = CGAffineTransformMakeRotation(CGFloat(-M_PI / 2.0))
        } else if orientation == UIDeviceOrientation.PortraitUpsideDown {
            t = CGAffineTransformMakeRotation(CGFloat(M_PI / 2.0))
        } else if (orientation == UIDeviceOrientation.LandscapeRight) {
            t = CGAffineTransformMakeRotation(CGFloat(M_PI))
        } else {
            t = CGAffineTransformMakeRotation(0)
        }
        outputImage = outputImage.imageByApplyingTransform(t)

        let cgImage = self.context.createCGImage(outputImage, fromRect: outputImage.extent)

        self.ciImage = outputImage

        dispatch_sync(dispatch_get_main_queue(), {


           (self.previewView.layer as! AVCaptureVideoPreviewLayer).contents = cgImage


        })
    }
}

最佳答案

https://github.com/jabson/MagicVideo

在项目中,过滤后的 CIImage 正在使用 CIContext 在 OpenGLView 上绘制,并且工作正常。也有写入照片库的可能性

关于ios - Swift iOS 实时对视频应用滤镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451784/

相关文章:

swift AVAudioEngine : Changing the Audio Input Device for MacOS

ios - AVPlayer/AVPlayerItem 如何通知我的应用程序无法访问的网络故障?

ios - 如何在不手动计算偏移量的情况下以编程方式堆叠 View ?

objective-c - 撤消 [UIGestureRecognizer* requireGestureRecognizerToFail]

ios - FBSDKCoreKit.framework/FBSDKCoreKit : no matching architecture in universal wrapper

ios - NWConnection 发送多个数据报

ios - 所有第三方音频单元均丢失

ios - 用户可以直接从 iOS 访问文件吗?

iphone - Google Analytics 不发送数据!获得 0 个活跃用户

ios - CollectionViewCell 不持久