http://www.iphones.ru/forum/index.php?showtopic=77707

testImageArray = [[[NSArray alloc] initWithObjects:
                                                                       
[UIImage imageNamed:@"1.png"],
                                                                       
[UIImage imageNamed:@"2.png"],
                                                                       
[UIImage imageNamed:@"3.png"],
                                                                       
[UIImage imageNamed:@"4.png"],
                                                                       
[UIImage imageNamed:@"5.png"],
                                                                 
nil]autorelease];

                       
[self writeImageAsMovie:testImageArray toPath:documentsDirectory size:CGSizeMake(460, 320) duration:1];


-(void)writeImageAsMovie:(NSArray *)array 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];
       
       
CVPixelBufferRef buffer = NULL;
       
//buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
        buffer
= [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage] size:CGSizeMake(460, 320)];
       
CVPixelBufferPoolCreatePixelBuffer (NULL, adaptor.pixelBufferPool, &buffer);
       
       
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
       
int i = 1;
       
while (writerInput.readyForMoreMediaData)
       
{
               
NSLog(@"inside for loop %d",i);
               
CMTime frameTime = CMTimeMake(1, 20);
               
               
CMTime lastTime=CMTimeMake(i, 20); //i is from 0 to 19 of the loop above
               
               
CMTime presentTime=CMTimeAdd(lastTime, frameTime);
               
               
if (i >= [array count])
               
{
                        buffer
= NULL;
               
}
               
else
               
{
                        buffer
= [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage] size:CGSizeMake(460, 320)];
               
}                
               
//CVBufferRetain(buffer);
               
               
if (buffer)
               
{
                       
// append buffer
                       
[adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
                        i
++;
               
}
               
else
               
{
                       
// done!
                       
                       
//Finish the session:
                       
[writerInput markAsFinished];
                       
[videoWriter finishWriting];                            
                       
                       
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
                       
[videoWriter release];
                       
[writerInput release];
                       
NSLog (@"Done");
//                      [imageArray removeAllObjects];                    
                       
break;
               
}
       
}
       
        path
= [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/myMovie.m4v"]];
       
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
       
       
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
       
NSParameterAssert(library);
       
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:[NSURL fileURLWithPath:path]]) {
               
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error){}];   //Тут ошибка вылетает
       
}
       
[library release];
}


      
Posted by k_ben