我使用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;
}
有什么建议吗?
答案 0 :(得分:2)
致电CGBitmapContextCreate
时,您应该通过kCGImageAlphaNoneSkipFirst
而不是kCGImageAlphaNoneSkipLast
。
这是因为您的位图布局为alpha然后是颜色,而不是颜色,而不是alpha。
这让你觉得蓝色的原因是你将ARG视为RGB。 A(alpha)为零,因此您的图像中没有红色。从图像中删除所有红色通常会给你一种蓝绿色调。