title: AVAssetImageGenerator fails at copying image
body: <p>I am using <code>AVAssetImageGenerator</code> to create an image from the last frame of a video. This usually works fine, but every now and then <code>copyCGImageAtTime</code> fails with the error </p>
  
  <pre><code>NSLocalizedDescription = "Cannot Open";
  NSLocalizedFailureReason = "This media cannot be used.";
  NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12431";
  </code></pre>
  
  <p>I am verifying that the AVAsset is not nil and I'm pulling the CMTime directly from the asset, so I do not understand why this keeps happening. This only happens when trying to get the last frame, if I use <code>kCMTimeZero</code> instead, it seems to work.</p>
  
  <pre><code>- (void)getLastFrameFromAsset:(AVAsset *)asset completionHandler:(void (^)(UIImage *image))completion
  {
      NSAssert(asset, @"Tried to generate last frame from nil asset");
      AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
      gen.requestedTimeToleranceBefore = kCMTimeZero;
      gen.requestedTimeToleranceAfter = kCMTimeZero;
      gen.appliesPreferredTrackTransform = YES;
      CMTime time = [asset duration];
      NSError *error = nil;
      CMTime actualTime;
  
      CGImageRef imageRef = [gen copyCGImageAtTime:time actualTime:&amp;actualTime error:&amp;error];
      UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
      NSAssert(image, @"Failed at generating image from asset's last frame");
      completion(image);
      CGImageRelease(imageRef);
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/11485608/generating-thumbnails-from-videos-created-using-avassetwriter">This</a> seems to be related, but it did not solve my problem.</p>