看來,使用EAGLContext
代替標準CIContext
就是答案。這爲我的需求創建彩色圖像提供了足夠快的性能。
簡單的代碼在這裏例如:
在初始化:
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject: [NSNull null] forKey: kCIContextWorkingColorSpace];
m_EAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
m_CIContext = [CIContext contextWithEAGLContext:m_EAGLContext options:options];
設置顏色:
-(void)setColorCorrection:(UIColor*)color
{
CGFloat r,g,b,a;
[color getRed:&r green:&g blue:&b alpha:&a];
CIVector *redVector = [CIVector vectorWithX:r Y:0 Z:0];
CIVector *greenVector = [CIVector vectorWithX:0 Y:g Z:0];
CIVector *blueVector = [CIVector vectorWithX:0 Y:0 Z:b];
m_ColorFilter = [CIFilter filterWithName:@"CIColorMatrix"];
[m_ColorFilter setDefaults];
[m_ColorFilter setValue:redVector forKey:@"inputRVector"];
[m_ColorFilter setValue:greenVector forKey:@"inputGVector"];
[m_ColorFilter setValue:blueVector forKey:@"inputBVector"];
}
在每個視頻幀:
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CGImageRef cgImage = nil;
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
[m_ColorFilter setValue:ciImage forKey:kCIInputImageKey];
CIImage *adjustedImage = [m_ColorFilter valueForKey:kCIOutputImageKey];
cgImage = [m_CIContext createCGImage:adjustedImage fromRect:ciImage.extent];
你已經採取了看一個GPUImage框架?它不會讓您遠離使用中間格式,但在大多數情況下,濾波器運行速度比CIFilters快很多。 https://github.com/BradLarson/GPUImage –
看起來像一個非常酷的圖書館整體,但不認爲它會做我所需要的,因爲我需要AVFoundation面部檢測以及顏色校正。 – Liron