我們使用UIImage中的AVAssetWriter導出視頻,這些視頻首先轉換爲CVPixelBufferRef,但圖像在視頻中由160像素高的黑色「酒吧」底部的空像素。iOS AVAssetWriter當從UIImages導出時視頻高度「Squished」
鑑於圖像是640x480,可以看到160像素的黑條 - 這就好像圖像被旋轉並且底部丟失了一樣 - 但是 - 當我們看視頻圖像時 - 高度相同圖像被捕獲 - 它只是「擠壓」,以適應黑色條。
下面是我們的UIImage到CVBufferPixelRef的代碼。
任何想法可能會導致此行爲?
- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
{
ATHSingleton *singleton = [ATHSingleton singletons];
int height = singleton.screenHeight;
int width = singleton.screenWidth;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, 480,
640, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
&pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, width,
height, 8, 4*width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
CGContextDrawImage(context, CGRectMake(0, -80, 480, 640), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}