title: Create video from images with same size (height and width of all images in given cgsize) in iphone
body: <p>I want to create video from images taken from gallery. I have stored images into an array. Now I want to create video from it with size user gives. I have written code but it displays black area if image is small then given size. Can anyone kindly help me to solve this?</p>
  
  <p>Any needful help will be appreciated.</p>
  
  <p>Code I'm using to generate video :</p>
  
  <pre><code>-(void) writeImagesToMovieAtPath:(NSString *)path withSize:(CGSize) size
  {    
      NSLog(@"Write Started");
  
      NSError *error = nil;
  
      AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                    [NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4
                                                                error:&amp;error];
      NSParameterAssert(videoWriter);
  
      NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                     AVVideoCodecH264, AVVideoCodecKey,
                                     [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                     [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                                     nil];
  
  
      AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
                                               assetWriterInputWithMediaType:AVMediaTypeVideo
                                               outputSettings:videoSettings];
  
  
  
  
      AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                       assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                       sourcePixelBufferAttributes:nil];
  
      NSParameterAssert(videoWriterInput);
  
      NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
      videoWriterInput.expectsMediaDataInRealTime = YES;
      [videoWriter addInput:videoWriterInput];
      //Start a session:
      [videoWriter startWriting];
      [videoWriter startSessionAtSourceTime:kCMTimeZero];
  
  
      //Video encoding
  
      CVPixelBufferRef buffer = NULL;
  
      //convert uiimage to CGImage.
  
      int frameCount = 0;
  
      for(int i = 0; i&lt;[arrSelectedImgs count]; i++)
      {
         // UIImage *img = [self scaleImage:[arrSelectedImgs objectAtIndex:i] toSize:size];
  
          buffer = [self pixelBufferFromCGImage:[[arrSelectedImgs objectAtIndex:i] CGImage] size:size];
  
          BOOL append_ok = NO;
          int j = 0;
          while (!append_ok &amp;&amp; j &lt; 30)
          {
              if (adaptor.assetWriterInput.readyForMoreMediaData)
              {
                  printf("appending %d attemp %d\n", frameCount, j);
  
                  CMTime frameTime = CMTimeMake(frameCount,(int32_t) 5);
  
                  append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
                  CVPixelBufferPoolRef bufferPool = adaptor.pixelBufferPool;
                  NSParameterAssert(bufferPool != NULL);
  
                  [NSThread sleepForTimeInterval:0.05];
              }
              else
              {
                  printf("adaptor not ready %d, %d\n", frameCount, j);
                  [NSThread sleepForTimeInterval:0.1];
              }
              j++;
          }
          if (!append_ok)
          {
              printf("error appending image %d times %d\n", frameCount, j);
          }
          frameCount++;
          CVBufferRelease(buffer);
      }
  
      [videoWriterInput markAsFinished];
      [videoWriter finishWriting];
  
      [arrSelectedImgs removeAllObjects];
  
      NSLog(@"Write Ended at path == %@",path);
  
      [self createGIFFromVideoURL:[NSURL URLWithString:path]];
  
      [self viewMovieAtUrl:[NSURL URLWithString:path]];
  }
  
  - (CVPixelBufferRef) pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
  {
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                               [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                               nil];
      CVPixelBufferRef pxbuffer = NULL;
      CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width,
                                            size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
                                            &amp;pxbuffer);
      status=status;//Added to make the stupid compiler not show a stupid warning.
      NSParameterAssert(status == kCVReturnSuccess &amp;&amp; pxbuffer != NULL);
  
      CVPixelBufferLockBaseAddress(pxbuffer, 0);
      void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
      NSParameterAssert(pxdata != NULL);
  
      CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
                                                   size.height, 8, 4*size.width, rgbColorSpace,
                                                   kCGImageAlphaNoneSkipFirst);
      NSParameterAssert(context);
  
      //CGContextTranslateCTM(context, 0, CGImageGetHeight(image));
      //CGContextScaleCTM(context, 1.0, -1.0);//Flip vertically to account for different origin
  
      CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                             CGImageGetHeight(image)), image);
      CGColorSpaceRelease(rgbColorSpace);
      CGContextRelease(context);
  
      CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
  
      return pxbuffer;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>