Ios CGBitmapContextCreate无效数据字节/行
我想用苹果开发者页面上的一个例子 现在,我使用了Ios CGBitmapContextCreate无效数据字节/行,ios,avfoundation,swift2,Ios,Avfoundation,Swift2,我想用苹果开发者页面上的一个例子 现在,我使用了imageFromSampleBuffer:sampleBuffer方法,如文档所述。我用Swift写作,所以我尽可能地从Obj-C转换为Swift 2 我忍不住收到有关无效数据字节/行的错误:对于8个整数位/组件、3个组件、kCGImageAlphaPremultipledFirst和CGBitmapContextCreateImage:无效上下文0x0,应至少为7680。 func imageFromSampleBuffer(sampleBuf
imageFromSampleBuffer:sampleBuffer
方法,如文档所述。我用Swift写作,所以我尽可能地从Obj-C转换为Swift 2
我忍不住收到有关无效数据字节/行的错误:对于8个整数位/组件、3个组件、kCGImageAlphaPremultipledFirst
和CGBitmapContextCreateImage:无效上下文0x0,应至少为7680。
func imageFromSampleBuffer(sampleBuffer: CMSampleBufferRef) -> UIImage {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, 0)
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!)
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let width = CVPixelBufferGetWidth(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue)
let qImage = CGBitmapContextCreateImage(context)
CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)
let image = UIImage(CGImage: qImage!)
return(image)
}
我就是这样称呼这个方法的:
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
print("Delegate called")
let image = imageFromSampleBuffer(sampleBuffer)
print(image)
}
我完全不知道如何进行
CGBitmapContextCreate
在以下位置创建:1:我确实检查了这个,在
CGBitmapContextCreate
中使用kgimagealphapremultipledfirst
似乎是正确的。2:我如何确认这一点?3:我怎么检查这个?图像直接来自相机,因此它应该有alpha组件,对吗?2:在创建AVCaptureOutput
时,您指定哪种格式?例如,什么是AVCaptureVideoDataOutput.videoSettings[kCVPixelBufferPixelFormatTypeKey]?3:相机不捕捉alpha组件。这些数据从何而来?2:我使用avcapturedevideodataoutput
,但我还没有设置。videoSettings
。我尝试设置output?.videoSettings[kCVPixelBufferPixelFormatTypeKey]=kCVPixelFormatType\u 32BGRA
,但错误是我无法将OSType
/UInt32
分配给任何对象?
。3:说实话,我真的不明白,对不起。你能解释一下吗?我希望你还能帮我吗?试着包装一下,…=Int(kCVPixelFormatType_32BGRA)
。