title: Objective -C How to create a video from an NSMutableArray?
body: <p>I have an <code>NSMutableArray</code> of images I need to create a video and save as .mp4 format 
  I tried a code found in many places to produce a video from images. But it does not work. It creates a blank file</p>
  
  <pre><code>-(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:&amp;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];
  
  
      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;
  
      //convert uiimage to CGImage.
  
      for (int i = 0; i &lt; [array count]; i++) {  //NSMutableArray
          buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:i] CGImage]];
          NSLog(@"%@",[[array objectAtIndex:i] CGImage]);
      }
      [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
  
      //Write samples:
  
  
      //Finish the session:
      [writerInput markAsFinished];
  }
  
  -(CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
  {
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:YES],   kCVPixelBufferCGImageCompatibilityKey,
                               [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                               nil];
      CVPixelBufferRef pxbuffer = NULL;
  
      CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, self.view.frame.size.width,
                                            self.view.frame.size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
                                            &amp;pxbuffer);
      NSParameterAssert(status == kCVReturnSuccess &amp;&amp; pxbuffer != NULL);
  
      CVPixelBufferLockBaseAddress(pxbuffer, 0);
      void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
      NSParameterAssert(pxdata != NULL);
  
      CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(pxdata, self.view.frame.size.width,
                                                   self.view.frame.size.height, 8, 4*self.view.frame.size.width, rgbColorSpace,
                                                   kCGImageAlphaNoneSkipFirst);
      NSParameterAssert(context);
      CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
      CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                             CGImageGetHeight(image)), image);
      CGColorSpaceRelease(rgbColorSpace);
      CGContextRelease(context);
  
      CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
  
      return pxbuffer;
  }
  </code></pre>
  
  <p>What is wrong?</p>
  
  <p>Can anyone help me to find a solution please?</p>
  
  <p>Is there any other way that I can create video from images?</p>