【问题标题】:Build a video from UIImages problems从 UIImages 问题构建视频
【发布时间】:2012-09-21 11:19:00
【问题描述】:

我想从 UIImages 数组创建电影。我试过这段代码:

-(void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size          duration:(int)duration 
{
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* writerInput = [[AVAssetWriterInput
                                    assetWriterInputWithMediaType:AVMediaTypeVideo
                                    outputSettings:videoSettings] retain];

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                 sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];

//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

//Write samples:
CVPixelBufferRef buffer = [Utils pixelBufferFromCGImage:image.CGImage size:size];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(duration-1, 2)];

//Finish the session:
[writerInput markAsFinished];
[videoWriter endSessionAtSourceTime:CMTimeMake(duration, 2)];
[videoWriter finishWriting];
}

但是输出不行。

图片是这样的:

http://imageshack.us/photo/my-images/854/screenshot20120921at140.png/

视频输出为:

http://imageshack.us/photo/my-images/856/screenshot20120921at140.png/

【问题讨论】:

  • 你遇到了什么错误?!!
  • 没有,但是输出的视频很乱。

标签: objective-c xcode video photo


【解决方案1】:

检查您为缓冲区指定的大小。 试试这个尺寸,看看一次:

buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage] size:CGSizeMake(480, 320)];

【讨论】:

  • 我试过了。那不是问题。 :(我也试过这个链接:developers-life.com/create-movie-from-array-of-images.html。同样的结果!
  • 如果你的图片尺寸大于或小于你给的尺寸,输出视频会有点乱。!!
  • 所有图像的大小相同。我给缓冲区图像的大小:[self writeImageAsMovie:image toPath:myPathDocs size:image.size duration:1];
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 2011-11-11
  • 2014-03-30
相关资源
最近更新 更多