Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 来自CMSampleBuffer的UIImage具有蓝色色调_Ios_Swift2_Avfoundation_Avcapture - Fatal编程技术网

Ios 来自CMSampleBuffer的UIImage具有蓝色色调

Ios 来自CMSampleBuffer的UIImage具有蓝色色调,ios,swift2,avfoundation,avcapture,Ios,Swift2,Avfoundation,Avcapture,我正在UIImageView中显示转换为UIImages的CMSampleBuffers的视频提要。在下面的照片中,背景层是AVCapturePreviewLayer,中间是缓冲区提要。我的目标是去除蓝色 以下是从CMSampleBuffer到UIImage的代码 extension CMSampleBuffer { func imageRepresentation() -> UIImage? { let imageBuffer: CVImageBufferRef = CM

我正在UIImageView中显示转换为UIImages的CMSampleBuffers的视频提要。在下面的照片中,背景层是AVCapturePreviewLayer,中间是缓冲区提要。我的目标是去除蓝色

以下是从CMSampleBuffer到UIImage的代码

extension CMSampleBuffer {
  func imageRepresentation() -> UIImage? {

    let imageBuffer: CVImageBufferRef = CMSampleBufferGetImageBuffer(self)!

    CVPixelBufferLockBaseAddress(imageBuffer, 0)
    let address = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
    let width = CVPixelBufferGetWidth(imageBuffer)
    let height = CVPixelBufferGetHeight(imageBuffer)

    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.NoneSkipFirst.rawValue)
    let imageRef = CGBitmapContextCreateImage(context)

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0)
    let resultImage: UIImage = UIImage(CGImage: imageRef!)

    return resultImage
  }
}
AVCaptureVideoDataOutput设置:

class MovieRecorder: NSObject {
  // vars
  private let captureVideoDataOutput = AVCaptureVideoDataOutput()

  // capture session boilerplate setup...

  captureVideoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)]
  captureVideoDataOutput.alwaysDiscardsLateVideoFrames = true
  captureVideoDataOutput.setSampleBufferDelegate(self, queue: captureDataOutputQueue)
}

问题在于bitmapInfo。这个位图信息修复了它

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let context = CGBitmapContextCreate(address, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)