Ios 来自CMSampleBuffer的UIImage具有蓝色色调
我正在UIImageView中显示转换为UIImages的CMSampleBuffers的视频提要。在下面的照片中,背景层是AVCapturePreviewLayer,中间是缓冲区提要。我的目标是去除蓝色 以下是从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
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)