我尝试编写ios相机,并且我采用了部分代码from apple:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// Create a UIImage from the sample buffer data
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
< Add your code here that uses the image >
}
我需要在程序的任何地方调用此函数。但是要求它创建一个(CMSampleBufferRef)
的对象类型。怎么做?
我试着写一些像:
buf1 = [[CMSampleBufferRef alloc]init]
但这是错误的方式。
答案 0 :(得分:4)
这是我目前用于在swift3中模拟CMSampleBuffer
单元测试的片段:
fileprivate func getCMSampleBuffer() -> CMSampleBuffer {
var pixelBuffer : CVPixelBuffer? = nil
CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)
var info = CMSampleTimingInfo()
info.presentationTimeStamp = kCMTimeZero
info.duration = kCMTimeInvalid
info.decodeTimeStamp = kCMTimeInvalid
var formatDesc: CMFormatDescription? = nil
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer!, &formatDesc)
var sampleBuffer: CMSampleBuffer? = nil
CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
pixelBuffer!,
formatDesc!,
&info,
&sampleBuffer);
return sampleBuffer!
}
答案 1 :(得分:0)
@Rotem Tamir 答案的 Swift 5 版本
fileprivate func getCMSampleBuffer() -> CMSampleBuffer {
var pixelBuffer: CVPixelBuffer?
CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)
var info = CMSampleTimingInfo()
info.presentationTimeStamp = CMTime.zero
info.duration = CMTime.invalid
info.decodeTimeStamp = CMTime.invalid
var formatDesc: CMFormatDescription?
CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault,
imageBuffer: pixelBuffer!,
formatDescriptionOut: &formatDesc)
var sampleBuffer: CMSampleBuffer?
CMSampleBufferCreateReadyWithImageBuffer(allocator: kCFAllocatorDefault,
imageBuffer: pixelBuffer!,
formatDescription: formatDesc!,
sampleTiming: &info,
sampleBufferOut: &sampleBuffer)
return sampleBuffer!
}