gpt4 book ai didi

ios - 如何将 UIImage 数组导出为电影?

转载 作者:IT王子 更新时间:2023-10-29 07:26:20 25 4
gpt4 key购买 nike

我有一个严重的问题:我有一个包含多个 UIImage 对象的 NSArray。我现在想做的是从那些 UIImages 创建电影。但我不知道该怎么做。

我希望有人能帮助我或向我发送一个代码片段来执行我想要的操作。

编辑:供将来引用 - 应用该解决方案后,如果视频看起来失真,请确保您捕获的图像/区域的宽度是 16 的倍数。经过数小时后发现奋斗在这里:
Why does my movie from UIImages gets distorted?

这是完整的解决方案(只需确保宽度是 16 的倍数)
http://codethink.no-ip.org/wordpress/archives/673

最佳答案

看看AVAssetWriter和其余的AVFoundation framework .作者有一个类型的输入 AVAssetWriterInput ,它又有一个名为 appendSampleBuffer: 的方法可以让您将单个帧添加到视频流中。基本上你必须:

1) 联系作者:

NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:somePath] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:640], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain]; //retain should be removed if ARC

NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];

2) 开始 session :

[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:…] //use kCMTimeZero if unsure

3) 写一些示例:

// Or you can use AVAssetWriterInputPixelBufferAdaptor.
// That lets you feed the writer input data from a CVPixelBuffer
// that’s quite easy to create from a CGImage.
[writerInput appendSampleBuffer:sampleBuffer];

4) 完成 session :

[writerInput markAsFinished];
[videoWriter endSessionAtSourceTime:…]; //optional can call finishWriting without specifying endTime
[videoWriter finishWriting]; //deprecated in ios6
/*
[videoWriter finishWritingWithCompletionHandler:...]; //ios 6.0+
*/

您仍然需要填写很多空白,但我认为剩下的唯一真正困难的部分是从 CGImage 获取像素缓冲区:

- (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
frameSize.height, kCVPixelFormatType_32ARGB, (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, frameSize.width,
frameSize.height, 8, 4*frameSize.width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, frameTransform);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

return pxbuffer;
}

frameSize 是一个描述目标帧大小的 CGSizeframeTransform 是一个 CGAffineTransform 可以让你转换当您将图像绘制到框架中时。

关于ios - 如何将 UIImage 数组导出为电影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3741323/

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