title: Convert UIView Layer to UIImage
body: <p>I'm playing Video using AVPlayerLayer in a View. I need to convert View to Image, I tried </p>
  
  <pre><code>[myview.layer renderInContext:context];
  </code></pre>
  
  <p>but this gives only black image. I wanna convert that view into image with video on that time. This conversion will occur simultaneously 0.05s. </p>
  
  <p>I tried with AVAssetImageGenerator. Which gives me the right image using Asset. But it taking little more time which makes some performance issue on my application. Can any one help me how to reduce the process of converting video to image for particular CMTime.</p>
  
  <p>Below are my coding. </p>
  
  <pre><code>- (UIImage *)currentItemScreenShot
  {
      AVPlayer *abovePlayer = [objVC player];
      if(imageGenerator == nil)
      {
          AVAsset *asset = [[[objVC player] currentItem] asset];
          imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
      }
  
      CMTime time = [[abovePlayer currentItem] currentTime];
      if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] &amp;&amp; [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
          [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
          [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
      }
  
      CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time
                                                 actualTime:NULL
                                                      error:NULL];
      if (imgRef == nil) {
          if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] &amp;&amp; [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
              [imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity];
              [imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity];
          }
          imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
      }
      UIImage *image = [UIImage imageWithCGImage:imgRef];
      CGImageRelease(imgRef);
  
      image = [self reverseImageByScalingToSize:image.size :image];
      return image;
  }
  </code></pre>