iOS YUV(i420) to UIImage

    思路:YUV(i420) -> YUV(NV12) -> CIImage -> UIImage
        获取到的YUV Data格式:
                (int width, 
                 int height, 
                 unsigned long long time_stamp,
                 unsigned char*yData, 
                 unsigned char* uData, 
                 unsigned char*vData,
                 int yStride, 
                 int uStride, 
                 int vStride)

将YUV分量填入buffer 得到标准buffer
I420: YYYYYYYY UU VV -> NV12: YYYYYYYY UVUV
附上YUV图解:https://blog.csdn.net/xjhhjx/article/details/80291465

        NSDictionary *pixelAttributes = @{(NSString *)kCVPixelBufferIOSurfacePropertiesKey:@{}};
        CVPixelBufferRef pixelBuffer = NULL;
        CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault,
                                              width,
                                              height,
                                              kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,   //  NV12
                                              (__bridge CFDictionaryRef)(pixelAttributes),
                                              &pixelBuffer);
        if (result != kCVReturnSuccess) {
            NSLog(@"Unable to create cvpixelbuffer %d", result);
        }
        CVPixelBufferLockBaseAddress(pixelBuffer, 0);
        unsigned char *yDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
        for (int i = 0, k = 0; i < height; i ++) {
            for (int j = 0; j < width; j ++) {
                yDestPlane[k++] = yData[j + i * yStride]; 
            }
        }
        unsigned char *uvDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
        for (int i = 0, k = 0; i < height / 2; i ++) {
            for (int j = 0; j < width / 2; j ++) {
                uvDestPlane[k++] = uData[j + i * uStride]; 
                uvDestPlane[k++] = vData[j + i * vStride]; 
            }
        }
CIImage Conversion
        CIImage *coreImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
        CIContext *MytemporaryContext = [CIContext contextWithOptions:nil];
        CGImageRef MyvideoImage = [MytemporaryContext createCGImage:coreImage
                                                           fromRect:CGRectMake(0, 0, width, height)];
UIImage Conversion
        UIImage *Mynnnimage = [[UIImage alloc] initWithCGImage:MyvideoImage
                                                         scale:1.0
                                                   orientation:UIImageOrientationUp];
        CVPixelBufferRelease(pixelBuffer);
        CGImageRelease(MyvideoImage);

你可能感兴趣的:(iOS YUV(i420) to UIImage)