initWithCVPixelBuffer:options:
在核心图像编程指南中,他们提到了这个功能
Take advantage of the support for YUV image in iOS 6.0 and later.
Camera pixel buffers are natively YUV but most image processing
algorithms expect RBGA data. There is a cost to converting between the
two. Core Image supports reading YUB from CVPixelBuffer objects and
applying the appropriate color transform.options = @{ (id)kCVPixelBufferPixelFormatTypeKey :
@(kCVPixelFormatType_420YpCvCr88iPlanarFullRange) };
但是,我无法正常使用它.我有一个原始的YUV数据.所以,这就是我所做的
void *YUV[3] = {data[0],data[1],data[2]}; size_t planeWidth[3] = {width,width/2,width/2}; size_t planeHeight[3] = {height,height/2,height/2}; size_t planeBytesPerRow[3] = {stride,stride/2,stride/2}; CVPixelBufferRef pixelBuffer = NULL; CVReturn ret = CVPixelBufferCreateWithPlanarBytes(kcfAllocatorDefault,width,height,kCVPixelFormatType_420YpCbCr8PlanarFullRange,nil,width*height*1.5,3,YUV,planeWidth,planeHeight,planeBytesPerRow,&pixelBuffer); NSDict *opt = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8PlanarFullRange) }; CIImage *image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:opt];
我的形象是零.我不知道我错过了什么.
编辑:
我在通话前添加了锁定和解锁基地址.另外,我转储了pixelbuffer的数据,以确保pixellbuffer能够正确保存数据.它看起来只是init调用有问题.仍然CIImage对象返回nil.
CVPixelBufferLockBaseAddress(pixelBuffer,0); CIImage *image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:opt]; CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
解决方法
Calling
CVPixelBufferCreateWithBytes()
orCVPixelBufferCreateWithPlanarBytes()
will result inCVPixelBuffers
that are not IOSurface-backed……To do that,you must specify
kCVPixelBufferIOSurfacePropertiesKey
in thepixelBufferAttributes
dictionary when creating the pixel buffer usingCVPixelBufferCreate()
.
NSDictionary *pixelBufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSDictionary dictionary],(id)kCVPixelBufferIOSurfacePropertiesKey,nil]; // you may add other keys as appropriate,e.g. kCVPixelBufferPixelFormatTypeKey,kCVPixelBufferWidthKey,kCVPixelBufferHeightKey,etc. CVPixelBufferRef pixelBuffer; CVPixelBufferCreate(... (CFDictionaryRef)pixelBufferAttributes,&pixelBuffer);
Alternatively,you can make IOSurface-backed
CVPixelBuffers
usingCVPixelBufferPoolCreatePixelBuffer()
from an existing pixel buffer pool,if thepixelBufferAttributes
dictionary provided toCVPixelBufferPoolCreate()
includeskCVPixelBufferIOSurfacePropertiesKey
.