2012-09-21 2 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 ]; –