简体   繁体   English

在Swift 2.0中创建CMSampleBuffer的副本

[英]Create a copy of CMSampleBuffer in Swift 2.0

This has been asked before, but something must have changed in Swift since it was asked. 以前曾经问过这个问题,但是有问题的话,Swift的内容一定有所改变。 I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. 我试图存储从AVCaptureSession返回的CMSampleBuffer对象,以便稍后处理。 After some experimentation I discovered that AVCaptureSession must be reusing its CMSampleBuffer references. 经过一些实验,我发现AVCaptureSession必须重用其CMSampleBuffer引用。 When I try to keep more than 15 the session hangs. 当我试图保持超过15时会话挂起。 So I thought I would make copies of the sample buffers. 所以我想我会复制样本缓冲区。 But I can't seem to get it to work. 但我似乎无法让它发挥作用。 Here is what I have written: 这是我写的:

var allocator: Unmanaged<CFAllocator>! = CFAllocatorGetDefault()
var bufferCopy: UnsafeMutablePointer<CMSampleBuffer?>
let err = CMSampleBufferCreateCopy(allocator.takeRetainedValue(), sampleBuffer, bufferCopy)
if err == noErr {
    bufferArray.append(bufferCopy.memory!)
} else {
    NSLog("Failed to copy buffer. Error: \(err)")
}

This won't compile because it says that Variable 'bufferCopy' used before being initialized . 这不会编译,因为它表示Variable 'bufferCopy' used before being initializedVariable 'bufferCopy' used before being initialized I've looked at many examples and they'll either compile and not work or they won't compile. 我看了很多例子,他们要么编译也不能工作,否则他们就不会编译。

Anyone see what I'm doing wrong here? 有谁看到我在这里做错了什么?

You can simply pass a CMSampleBuffer? 你可以简单地传递一个CMSampleBuffer? variable (which, as an optional, is implicitly initialized with nil ) as inout argument with & : 变量(作为一个可选项,用nil隐式初始化)作为与&参数inout:

var bufferCopy : CMSampleBuffer?
let err = CMSampleBufferCreateCopy(kCFAllocatorDefault, buffer, &bufferCopy)
if err == noErr {
    // ...
}

Literally you're attempting to use the variable bufferCopy before it is initialized. 从字面上看,您尝试在初始化之前使用变量bufferCopy。

You've declared a type for it, but haven't allocated the memory it's pointing to. 你已经为它声明了一个类型,但没有分配它所指向的内存。

You should instead create CMSampleBuffers using the following call https://developer.apple.com/library/tvos/documentation/CoreMedia/Reference/CMSampleBuffer/index.html#//apple_ref/c/func/CMSampleBufferCreate 您应该使用以下调用创建CMSampleBuffers :https://developer.apple.com/library/tvos/documentation/CoreMedia/Reference/CMSampleBuffer/index.html#//apple_ref/c/func/CMSampleBufferCreate

You should be able to copy the buffer into this then (as long as the format of the buffer matches the one you're copying from). 您应该能够将缓冲区复制到此中(只要缓冲区的格式与您要复制的格式匹配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM