title: Create a gif with UIImages
body: <p>I am referring to this <a href="https://stackoverflow.com/questions/14915138/create-and-and-export-an-animated-gif-via-ios">post</a> . I am trying to make a gif file with the images created by screenshot. I am using a timer to create the snapshot of the screen,so that I get required amount of frames that could be used to create a gif. I take snaphots every 0.1 seconds (I will later end this timer after 3 seconds).</p>
  
  <p>Here is my code to take snapshots of my UIView:</p>
  
  <pre><code>-(void)recordScreen{
  
     self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES];
  
  }
  
  -(void)takeSnapShot{
  
      //capture the screenshot of the uiimageview and save it in camera roll
      UIGraphicsBeginImageContext(self.drawView.frame.size);
      [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
  }
  </code></pre>
  
  <p>and the post I am referring to, shows a helper function to create gif. I am not sure how should I pass my images to the helper function. Here is what I tried:</p>
  
  <p>I tried to modify this part :</p>
  
  <pre><code> static NSUInteger kFrameCount = 10;
   for (NSUInteger i = 0; i &lt; kFrameCount; i++) {
          @autoreleasepool {
              UIImage *image = [self takeSnaphot];
              CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
          }
      }
  </code></pre>
  
  <p>This creates the gif  with 10 frames of my <code>UIView</code>.</p>
  
  <p>Now....</p>
  
  <p><strong>What I am trying to do is:</strong></p>
  
  <p>I am drawing a simple drawing with finger on my <code>UIView</code> using <code>UIBeizerPath</code> and I am taking snapshots in parallel to my drawing ,so that I will have around 50-100 of PNG files. I am trying to pass all these images to the  makeGifMethod.</p>
  
  <p><strong>WorkFlow:</strong></p>
  
  <ol>
  <li>Start the App.</li>
  <li>Tap on a button which starts the timer, and take pics for every 0.1 sec</li>
  <li>Draw with finger (so all the drawing will be captured evry 0.1 secs)</li>
  <li>After each snapshot, I call <code>makeanimatedgif</code> method,so that the snapshot taken will be added to the previous frame in the gif file</li>
  <li>Stop everything</li>
  </ol>
  
  <p><strong>Issue:</strong></p>
  
  <p>-<strong>Case 1:</strong>  </p>
  
  <ol>
  <li>Tap on button (which does create gif immediatley)</li>
  <li>Start drawing</li>
  <li>Stop </li>
  <li>Check gif (a gif with 10 frames is created with white background,since I drew nothing when I hit button)</li>
  </ol>
  
  <p>If I call my snapShot method in a loop, I get the last 10 frames of my drawing,but not everything.</p>
  
  <pre><code>for (NSUInteger i = 0; i &lt; 10; i++) {
          @autoreleasepool {
              UIImage *image = [self takeSnapShot];
              CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
          }
      }
  </code></pre>
  
  <p>-<strong>Case 2:</strong></p>
  
  <ol>
  <li>Hit button (start timer, take snapshot, call makeGifMethod in parallel to it)</li>
  <li>Draw somthing</li>
  <li>Stop</li>
  <li>Check gif (empty gif is created without any frames,I beleieve calling makeGifMethod every 0.1 seconds didnt work as expected)</li>
  </ol>
  
  <p>Here is the code for <strong>case 2:</strong></p>
  
  <pre><code>  -(void)recordScreen{
  
     self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES];
  
  }
  
   -(void)takeSnapShot{
  
      //capture the screenshot of the uiimageview and save it in camera roll
      UIGraphicsBeginImageContext(self.drawView.frame.size);
      [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          [self makeAnimatedGif:(UIImage *)viewImage];
  
      });
  
  } 
  
  
  -(void) makeAnimatedGif:(UIImage *)image{
  
      NSDictionary *fileProperties = @{
                                       (__bridge id)kCGImagePropertyGIFDictionary: @{
                                               (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
                                               }
                                       };
  
      NSDictionary *frameProperties = @{
                                        (__bridge id)kCGImagePropertyGIFDictionary: @{
                                                (__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
                                                }
                                        };
  
      documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
      fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
  
      CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, 10, NULL);
      CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
  
              CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
  
      if (!CGImageDestinationFinalize(destination)) {
          NSLog(@"failed to finalize image destination");
      }
      CFRelease(destination);
  
      NSLog(@"url=%@", fileURL);
  }
  </code></pre>
  
  <p>Can someone please suggest me how to pass the captured images to the above method to make a gif?</p>