YUV转换RGB -- iOS版

1,260 阅读1分钟

iOS专用静态库libYUV,GitHub地址https://github.com/PangDuTechnology/AFOlibyuv.git

#pragma mark ------ 420P -> RGBA -> CGImageRef -> image

+ (void)makeYUVToRGB:(AVFrame *)avFrame

width:(int)inWidth

height:(int)inHeight

scale:(int)scale

block:(void (^)(UIImage *image))block{

uint8 *argb = (uint8 *) malloc(inWidth * inHeight * 4 *3 * sizeof(uint8));

I420ToBGRA(avFrame ->data[0],

avFrame ->linesize[0],

avFrame ->data[1],

avFrame ->linesize[1],

avFrame ->data[2],

avFrame ->linesize[2],

argb,

inWidth * 4,

inWidth,

inHeight);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(argb, inWidth, inHeight, 8, inWidth * 4 , colorSpace,kCGImageAlphaPremultipliedFirst);

CGImageRef quartzImage = CGBitmapContextCreateImage(context);

UIImage *image = [UIImage imageWithCGImage:quartzImage scale:scale orientation:UIImageOrientationUp];

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

CGImageRelease(quartzImage);

free(argb);

argb = NULL;

block(image);

}