2012-09-21 63 views
1

我想從UIImages陣列創建電影。我試過這個代碼:從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/

+0

你做了什麼錯誤!? –

+0

沒有,但輸出的視頻是一團糟。 –

回答

1

檢查你給緩衝區的大小。 嘗試這種規模和看一次:

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

我試過了。這不是問題。 :( 我也試過這個鏈接:http://www.developers-life.com/create-movie-from-array-of-images.html。相同的結果! –

+0

輸出視頻會有點混亂,如果大小你的圖片大小可能會大於或者小於你給出的尺寸!! –

+0

所有的圖片都有相同的大小我給緩衝區的圖像大小:[self writeImageAsMovie:image toPath:myPathDocs size:image.size duration:1 ]。 –