我有一个严重的问题:我有NSArray几个UIImage对象.我现在想做的是从那些创作电影UIImages.但我不知道该怎么做.
我希望有人可以帮助我或给我一个代码片段,它可以做我喜欢的事情.
编辑:供将来参考 - 应用解决方案后,如果视频看起来扭曲,请确保您捕获的图像/区域的宽度是16的倍数.在这里经过几个小时的努力后发现:
为什么我的UIImages电影得到了扭曲?
这是完整的解决方案(只需确保宽度是16的倍数)
http://codethink.no-ip.org/wordpress/archives/673
我必须从我的iPhone应用程序导出一部电影,其中包含来自NSArray的UIImage,并添加一些必须在预先指定的时间开始的.caf格式的音频文件.现在我已经能够使用AVAssetWriter导出包含图像的视频部分(经历就这个问题和其他网站的许多问题和答案后),但不能似乎找到一个方法来添加音频文件来完成电影.
这是我到目前为止所得到的
-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size
{
NSLog(@"Write Started");
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
//convert uiimage to CGImage.
int frameCount = 0; …Run Code Online (Sandbox Code Playgroud) 我想从图像创建一个视频.
所以,我是参考链接的来源.
链接:CVPixelBufferPool错误(kCVReturnInvalidArgument/-6661)
func writeAnimationToMovie(path: String, size: CGSize, animation: Animation) -> Bool {
var error: NSError?
let writer = AVAssetWriter(URL: NSURL(fileURLWithPath: path), fileType: AVFileTypeQuickTimeMovie, error: &error)
let videoSettings = [AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: size.width, AVVideoHeightKey: size.height]
let input = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)
let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input, sourcePixelBufferAttributes: nil)
input.expectsMediaDataInRealTime = true
writer.addInput(input)
writer.startWriting()
writer.startSessionAtSourceTime(kCMTimeZero)
var buffer: CVPixelBufferRef
var frameCount = 0
for frame in animation.frames {
let rect = CGRectMake(0, 0, size.width, size.height)
let rectPtr = UnsafeMutablePointer<CGRect>.alloc(1)
rectPtr.memory …Run Code Online (Sandbox Code Playgroud)