Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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 CGBitmapContextCreate无效数据字节/行_Ios_Avfoundation_Swift2 - Fatal编程技术网

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
    在以下位置创建:
  • 检查捕获的缓冲区是否为8位/分量RGB,而不是其他格式
  • 检查输入缓冲区是否缺少alpha组件。那么您应该使用KCGIMAGEALPHONE

  • 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)