首页 > 代码库 > iOS开发之视频根据url获取第一帧图片,获取任一帧图片

iOS开发之视频根据url获取第一帧图片,获取任一帧图片

+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {  AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];  

    NSParameterAssert(asset);  

  AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];  

     assetImageGenerator.appliesPreferredTrackTransform = YES;  

   assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;  

      CGImageRef thumbnailImageRef = NULL;  

  CFTimeInterval thumbnailImageTime = time;  

  NSError *thumbnailImageGenerationError = nil;  

     thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];  

  if(!thumbnailImageRef)  

      NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);  

      UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;  

    return thumbnailImage;  

iOS开发之视频根据url获取第一帧图片,获取任一帧图片