yuv转换RGB,转换UIImage

void FFDecoder::setupScaler()
{
//    sws_freeContext(img_convert_ctx);
	// Allocate RGB picture
	avpicture_alloc(&picture, PIX_FMT_RGB24, pCodecCtx->width , pCodecCtx->height);
	
	// Setup scaler
	static int sws_flags =  SWS_FAST_BILINEAR;
	img_convert_ctx = sws_getContext(pCodecCtx->width,
									 pCodecCtx->height,
									 pCodecCtx->pix_fmt,
									 pCodecCtx->width,
									 pCodecCtx->height,
									 PIX_FMT_RGB24,
									 sws_flags, NULL, NULL, NULL);
}

void FFDecoder::convertFrameToRGB()
{
    //to RGB
    sws_scale (img_convert_ctx, pFrame->data, pFrame->linesize,
			   0, pCodecCtx->height,
			   picture.data, picture.linesize);
    
    //to UIImage
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, picture.data[0], picture.linesize[0]*pCodecCtx->height,kCFAllocatorNull);
	CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGImageRef cgImage = CGImageCreate(pCodecCtx->width,
									   pCodecCtx->height,
									   8,
									   24,
									   picture.linesize[0],
									   colorSpace,
									   bitmapInfo,
									   provider,
									   NULL,
									   NO,
									   kCGRenderingIntentDefault);
	CGColorSpaceRelease(colorSpace);
	UIImage *image = [UIImage imageWithCGImage:cgImage];
    
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
	CGImageRelease(cgImage);
	CGDataProviderRelease(provider);
	CFRelease(data);
}

  

posted @ 2013-11-28 15:04  暖流  阅读(2401)  评论(0编辑  收藏  举报