gpt4 book ai didi

ios - iOS 5 : UIView converted to video and the resulting video is corrupted

转载 作者:行者123 更新时间:2023-12-01 19:23:39 25 4
gpt4 key购买 nike

我想获取UIView,将其转换为图像,然后将该图像存储在视频文件(.mp4)中。
我使用代码的下一部分来抓取图像并将其放入像素缓冲区:

BOOL appended;
if(input.readyForMoreMediaData==YES){
//grab the view and convert it into image
CGSize imgsize=self.imageSource.frame.size;
UIGraphicsBeginImageContext(imgsize);
[self.imageSource.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* grabbedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CVReturn cvErr = kCVReturnSuccess;
CGImageRef image = (CGImageRef) [grabbedImage CGImage];

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;

CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, imgsize.width,
imgsize.height, 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, imgsize.width,
imgsize.height, 8, 4*imgsize.width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

appended = [pxlBufAdaptor appendPixelBuffer:pxbuffer withPresentationTime:presentationTime];
CVBufferRelease(pxbuffer );
}

问题在于,生成的视频包含损坏的图像-所有像素都已偏移。看起来内存中充满了带有一些偏移量的字节,并且该偏移量破坏了演示文稿。

如何解决?
我想要任何胶水或方向。
提前致谢。

最佳答案

这看起来很可疑:

CGContextRef context = CGBitmapContextCreate(pxdata, imgsize.width,
imgsize.height, 8, 4*imgsize.width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);

您正在根据图像宽度计算 bytesPerRow参数,而不是向 pxbuffer询问其每行字节数。试试这个:
CGContextRef context = CGBitmapContextCreate(pxdata, imgsize.width,
imgsize.height, 8, CVPixelBufferGetBytesPerRow(pxbuffer),
rgbColorSpace, kCGImageAlphaNoneSkipFirst);

同样,使用 UIGraphicsGetCurrentContext创建位图图形上下文,将图层渲染到上下文中,从上下文中获取图像,破坏上下文,创建像素缓冲区,使用像素缓冲区创建位图图形上下文并绘制该图层的图像进入新的上下文。为什么不将您的 CGContextDrawImage调用替换为 [self.imageSource.layer renderInContext:context]呢?

关于ios - iOS 5 : UIView converted to video and the resulting video is corrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8964695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com