Экспорт массива UIImage в видео - голубоватый результат
Я использую AVAssetWriter
с AVAssetWriterInputPixelBufferAdaptor
сделать видео из массива UIImage
, Получившийся фильм воспроизводится нормально, но с голубым оттенком. Я знаю, что формат пикселей не правильный, но не знаю достаточно, чтобы решить проблему.
AVAssetWriterInputPixelBufferAdaptor
код:
NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
генерировать CVPixelBufferRef
от CGImageRef
+ (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
{
CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
NSDictionary *options = @{
(__bridge NSString *)kCVPixelBufferCGImageCompatibilityKey: @(NO),
(__bridge NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @(NO)
};
CVPixelBufferRef pixelBuffer;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
frameSize.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
&pixelBuffer);
if (status != kCVReturnSuccess) {
return NULL;
}
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
void *data = CVPixelBufferGetBaseAddress(pixelBuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data, frameSize.width, frameSize.height,
8, CVPixelBufferGetBytesPerRow(pixelBuffer), rgbColorSpace,
(CGBitmapInfo) kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
return pixelBuffer;
}
Любое предложение?
1 ответ
При звонке CGBitmapContextCreate
ты должен пройти kCGImageAlphaNoneSkipFirst
и не kCGImageAlphaNoneSkipLast
,
Это потому, что ваши растровые изображения представлены как альфа, а не цвет, а не как альфа.
Причина, по которой вы получили голубоватый оттенок, в том, что вы рассматривали ARG как RGB. А (альфа) была нулевой, поэтому на вашем изображении не было красного цвета. Удаление всего красного с изображения, в общем, даст вам голубовато-зеленый оттенок.