【发布时间】:2013-10-21 17:18:23
【问题描述】:
我正在编写一个 iPhone 应用程序,它使用 AVFoundation 从相机创建静止图像。
阅读编程指南,我发现了几乎我需要做的代码,所以我正在尝试“逆向工程”并理解它。
我在理解将 CMSampleBuffer 转换为图像的部分时遇到了一些困难。
所以这是我所理解的,后来是代码。
CMSampleBuffer 表示内存中的一个缓冲区,其中存储了带有附加数据的图像。稍后我调用函数 CMSampleBufferGetImageBuffer() 来接收仅包含图像数据的 CVImageBuffer。
现在有一个函数我没看懂只能想象它的作用:CVPixelBufferLockBaseAddress(imageBuffer, 0);我不明白它是“线程锁”以避免对其进行多次操作还是锁定缓冲区地址以避免操作期间的更改(为什么要更改?..另一个帧,不是数据复制在另一个位置?)。其余的代码对我来说很清楚。
尝试在谷歌上搜索,但仍然没有找到任何有用的东西。
有人可以带点光吗?
-(UIImage*) getUIImageFromBuffer:(CMSampleBufferRef) sampleBuffer{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];
// Release the Quartz image
CGImageRelease(quartzImage);
return (image);
}
谢谢, 安德烈亚
【问题讨论】:
标签: iphone image ios4 avfoundation capture