在Swift 2.0中创建CMSampleBuffer的副本 [英] Create a copy of CMSampleBuffer in Swift 2.0

查看:107
本文介绍了在Swift 2.0中创建CMSampleBuffer的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前曾经问过这个问题,但是自从被问到以后,Swift的内容一定有所改变。我正在尝试存储从 AVCaptureSession 返回的 CMSampleBuffer 对象,以便稍后处理。经过一些实验,我发现 AVCaptureSession 必须重用其 CMSampleBuffer 引用。当我试图保持超过15时,会话挂起。所以我想我会复制样本缓冲区。但我似乎无法让它发挥作用。这是我写的:

This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. After some experimentation I discovered that AVCaptureSession must be reusing its CMSampleBuffer references. When I try to keep more than 15 the session hangs. 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)")
}

这不会编译,因为它表示在初始化之前使用变量'bufferCopy'。我看了很多例子,他们要么编译也不能工作,否则他们就不会编译。

This won't compile because it says that Variable '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?

推荐答案

字面意思是你在初始化之前尝试使用变量bufferCopy。

Literally you're attempting to use the variable bufferCopy before it is initialized.

你已经为它声明了一个类型,但还没有分配它所指向的内存。

You've declared a type for it, but haven't allocated the memory it's pointing to.

你应该使用以下调用创建CMSampleBuffers https:// developer。 apple.com/library/tvos/documentation/CoreMedia/Reference/CMSampleBuffer/index.html#//apple_ref/c/func/CMSampleBufferCreate

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

你应该然后能够将缓冲区复制到此(只要缓冲区的格式与您要复制的格式匹配)。

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

这篇关于在Swift 2.0中创建CMSampleBuffer的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆