如何把YUV转换成CIImage for iOS

我正在尝试将YUV图像转换为CIIMage,最后是UIImage。 我是相当新手,试图找出一个简单的方法来做到这一点。 从我学到的东西,从iOS6 YUV可以直接用来创buildCIImage,但正如我想创build它,CIImage只有一个零值。 我的代码是这样的 – >

NSLog(@"Started DrawVideoFrame\n"); CVPixelBufferRef pixelBuffer = NULL; CVReturn ret = CVPixelBufferCreateWithBytes( kCFAllocatorDefault, iWidth, iHeight, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, lpData, bytesPerRow, 0, 0, 0, &pixelBuffer ); if(ret != kCVReturnSuccess) { NSLog(@"CVPixelBufferRelease Failed"); CVPixelBufferRelease(pixelBuffer); } NSDictionary *opt = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) }; CIImage *cimage = [CIImage imageWithCVPixelBuffer:pixelBuffer options:opt]; NSLog(@"CURRENT CIImage -> %p\n", cimage); UIImage *image = [UIImage imageWithCIImage:cimage scale:1.0 orientation:UIImageOrientationUp]; NSLog(@"CURRENT UIImage -> %p\n", image); 

这里的lpData是YUV数据,它是一个无符号字符数组。

这也看起来很有趣: vImageMatrixMultiply ,在这个找不到任何例子。 谁能帮我这个?

我也面临这个类似的问题。 我试图将YUV(NV12)格式的数据显示到屏幕上。 这个解决scheme在我的项目中工作…

 //YUV(NV12)-->CIImage--->UIImage Conversion NSDictionary *pixelAttributes = @{kCVPixelBufferIOSurfacePropertiesKey : @{}}; CVPixelBufferRef pixelBuffer = NULL; CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault, 640, 480, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, (__bridge CFDictionaryRef)(pixelAttributes), &pixelBuffer); CVPixelBufferLockBaseAddress(pixelBuffer,0); unsigned char *yDestPlane = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); // Here y_ch0 is Y-Plane of YUV(NV12) data. memcpy(yDestPlane, y_ch0, 640 * 480); unsigned char *uvDestPlane = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1); // Here y_ch1 is UV-Plane of YUV(NV12) data. memcpy(uvDestPlane, y_ch1, 640*480/2); CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); if (result != kCVReturnSuccess) { NSLog(@"Unable to create cvpixelbuffer %d", result); } // CIImage Conversion CIImage *coreImage = [CIImage imageWithCVPixelBuffer:pixelBuffer]; CIContext *MytemporaryContext = [CIContext contextWithOptions:nil]; CGImageRef MyvideoImage = [MytemporaryContext createCGImage:coreImage fromRect:CGRectMake(0, 0, 640, 480)]; // UIImage Conversion UIImage *Mynnnimage = [[UIImage alloc] initWithCGImage:MyvideoImage scale:1.0 orientation:UIImageOrientationRight]; CVPixelBufferRelease(pixelBuffer); CGImageRelease(MyvideoImage); 

这里我展示了YUV(NV12)数据的数据结构,以及如何获得用于创buildCVPixelBufferRef的Y平面(y_ch0)和UV平面(y_ch1)。 我们来看看YUV(NV12)的数据结构。 在这里输入图像说明 如果我们看图片,我们可以得到关于YUV(NV12)的信息,

  • 总帧尺寸=宽度*高度* 3/2,
  • Y平面尺寸=框架尺寸* 2/3,
  • UV平面尺寸=框架尺寸* 1/3,
  • 存储在Y平面中的数据 – > {Y1,Y2,Y3,Y4,Y5 …..}。
  • U平面 – >(U1,V1,U2,V2,U3,V3,……)。

我希望这会对所有人有所帮助。 :)玩IOS开发:D