- (CVPixelBufferRef)RGB2PixelBuffer{
int width = buf_Width;
int height = buf_Height;
if (!resultBuffer) {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef) options,
&resultBuffer);
}
int bytesPerPixel = 4;
CGContextRef context = CGBitmapContextCreate(rgbBuffer, width, height, 8, width * bytesPerPixel, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipFirst);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
//转为PixelBuffer
CVPixelBufferLockBaseAddress(resultBuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(resultBuffer);
context = CGBitmapContextCreate(pxdata,
width,
height,
8,
CVPixelBufferGetBytesPerRow(resultBuffer),
colorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
CGContextConcatCTM(context, CGAffineTransformIdentity);
CGContextDrawImage(context, CGRectMake(0,0,width,height),quartzImage);
CGContextRelease(context);
CGImageRelease(quartzImage);
CVPixelBufferUnlockBaseAddress(resultBuffer, 0);
return resultBuffer;
}
网友评论