Last active
December 10, 2015 03:36
-
-
Save phucnm/9a91fda097eebeb6df52 to your computer and use it in GitHub Desktop.
Get thumnail of a video from a URL at arbitrary time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import AVFoundation | |
- (UIImage*)thumbnailFromVideo:(NSURL *)url atTime:(float)time { | |
AVAsset *asset = [AVAsset assetWithURL:url]; | |
// get 3 decimal places | |
// ex: 3.39235350 --> 3.392 | |
float nearest = roundf(time * 1000) / 1000; | |
// make CMTime from float | |
// because of 3 decimal places, we use 10000 (10^4) | |
// n decimal places, use 10^(n+1) | |
CMTime thumbnailTime = CMTimeMake(nearest, 10000); | |
// Get image from the video at the given time | |
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; | |
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL]; | |
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; | |
CGImageRelease(imageRef); | |
return thumbnail; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment