且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何转换YUV至iOS版CIImage

更新时间:2023-11-10 13:10:28

我也面临过类似的问题。我试图将YUV(NV12)格式的数据显示到屏幕上。此解决方案适用于我的项目......

I have also faced with this similar problem. I was trying to Display YUV(NV12) formatted data to the screen. This solution is working in my project...

//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)数据的数据结构以及我们如何获得Y-Plane (y_ch0)和UV-Plane(y_ch1),用于创建CVPixelBufferRef。让我们看一下YUV(NV12)数据结构。
如果我们查看图片,我们可以获得有关YUV(NV12)的信息,

Here I am showing data structure of YUV(NV12) data and how we can get the Y-Plane(y_ch0) and UV-Plane(y_ch1) which is used to create CVPixelBufferRef. Let's look at the YUV(NV12) data structure.. If we look at the picture we can get following information about YUV(NV12),


  • 总框架尺寸=宽度*高度* 3/2,

  • Y平面尺寸=框架尺寸* 2 / 3,

  • 紫外线平面尺寸=框架尺寸* 1/3,

  • 存储在Y平面中的数据 - > {Y1,Y2, Y3,Y4,Y5 .....}。

  • U-Plane - >(U1,V1,U2,V2,U3,V3,......}。

  • Total Frame Size = Width * Height * 3/2,
  • Y-Plane Size = Frame Size * 2/3,
  • UV-Plane size = Frame Size * 1/3,
  • Data stored in Y-Plane -->{Y1, Y2, Y3, Y4, Y5.....}.
  • U-Plane-->(U1, V1, U2, V2, U3, V3,......}.

我希望它对所有人都有帮助。:)享受IOS开发的乐趣:D

I hope it will be helpful to all. :) Have fun with IOS Development :D