从一组图像创建视频

时间:2012-01-24 12:51:41

标签: iphone objective-c uiimage avfoundation

我想从一组图片中创建一个视频,其中我参考了我的大部分代码:

How do I export UIImage array as a movie?

我写的代码是:

NSError *error = nil;

NSLog(@"Gonna start writing");

writer = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:@"ravi4"] fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(writer);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey, [NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, nil];

AVAssetWriterInput *writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                 sourcePixelBufferAttributes:nil];

NSParameterAssert(writerInput);
NSParameterAssert([writer canAddInput:writerInput]);
[writer addInput:writerInput];
[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];

UIImage *image = [UIImage imageNamed:@"back.jpg"];
UIImage *image2 = [UIImage imageNamed:@"arthas.jpg"];

UIImage *resizedImage = [ImageToVideoViewController imageWithImage:image scaledToSize:CGSizeMake(640, 480)];
UIImage *resizedImage2 = [ImageToVideoViewController imageWithImage:image2 scaledToSize:CGSizeMake(640, 480)];

CGImageRef img = [resizedImage CGImage];
CGImageRef img2 = [resizedImage2 CGImage];  

CVPixelBufferRef buffer = [ImageToVideoViewController pixelBufferFromCGImage:img size:CGSizeMake(640, 480)];
CVPixelBufferRef buffer2 = [ImageToVideoViewController pixelBufferFromCGImage:img2 size:CGSizeMake(640, 480)];

[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(0, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(3,1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {

}
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(6, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(9, 1)];

while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(10, 1)];
//[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(10, 2)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
CVBufferRelease(buffer);
CVBufferRelease(buffer2);

[writerInput markAsFinished];
//[writerInput finishWriting];
[writerInput release];

[writer endSessionAtSourceTime:CMTimeMake(10, 1)];
[writer finishWriting];
[writer release];

代码很乱,因为我只是试验如何解决这个问题

除了我试图显示的最后一张图片外,视频正常 视频一旦需要显示该图像就冻结并停止,但在到达该点之前一直正常工作

谢谢!

1 个答案:

答案 0 :(得分:0)

实际的解决方案是,您需要在屏幕上显示最终图像的持续时间内添加一个额外的帧。

您可以有效地将图像添加两次,首先是在您希望它出现的开始时间偏移处,然后再在您希望电影完成的时间偏移处再次添加。

否则,您将在显示最终图像的同时结束电影。