I am trying to acquire a thumbnail (of the first frame) from a video taken from iphone 3GS camera so I can display it. How to do this?
-
http://stackoverflow.com/questions/7501413/create-thumbnail-from-a-video-url-in-iphone-sdk/9478878#9478878 – Faizan Refai Jul 01 '12 at 06:02
-
possible duplicate of [Thumbnail image of video](http://stackoverflow.com/questions/8906004/thumbnail-image-of-video) – Avt Aug 08 '15 at 21:30
12 Answers
-(UIImage *)generateThumbImage : (NSString *)filepath
{
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}
you can get the frame using the time.value suppose you want to 1 second frame then use the
time.value = 1000 //Time in milliseconds

- 5,531
- 7
- 38
- 76

- 1,189
- 9
- 25
-
4@Diken Shah How Do i Get a thumbnail at n th second of the video. I set time.value=n but still i get the first frame – Pranav Jaiswal Feb 10 '14 at 12:28
-
To reliably get a thumbnail for different time indexes you need to set the requestedTimeTolerance properties on the imageGenerator, as detailed in this answer. https://stackoverflow.com/questions/39525253/generate-images-from-avassetimagegenerator-gives-same-image-duplicates-for-diffe – danfordham Mar 15 '20 at 15:28
The answer to this question is that one can now with 4.0 iOS get thumbnails using AVFoundation, the following code where the class property url is the movie url, will do the trick (you can get the thumbnail at any time, in the example its at time 0)
-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

- 22,363
- 9
- 64
- 71
-
7That code works great. The only downside of AVAssettImageGenerator is that presently it uses keyframes, which are typically spaced at one-second intervals -- so you can't get any finer resolution than that. If you're just trying to make some thumbnails to spread out over a timeline it's probably fine, but if you're trying to read consecutive frames, this won't work. In that case you have to use AVAssetReader, which has some quirks of its own, or MPMovieController, which has thumbnail methods that are frame-accurate. – Andy Milburn Feb 26 '11 at 22:51
-
4Appreciate this thread is old, but just want to add that MPMoviePlayerController (see below) is a much faster option. The code above takes a good 5-10 seconds to generate a thumbnail, whereas MPMovie is almost instant. – GuybrushThreepwood Apr 21 '12 at 14:37
-
I have try the above method to get thumb image from video but is not giving all thumb like at time 1.5 . @AndyMilburn can you give any sample link to get thumb using AVAssetReader. – Crazy Developer Apr 30 '12 at 08:46
-
1@AndyMilburn That's not exactly true, if you set requestedTimeToleranceBefore/After to smaller values you can get more closely spaced thumbnails. – Nick Feb 11 '13 at 02:26
-
Gives me the error AVErrorOperationNotSupportedForAsset . plz help – ruyamonis346 Aug 01 '13 at 12:07
-
2@GuybrushThreepwood I've tried this method with 5 different videos at the same time and all thumbnails are ready within a second; one thing to note is that you should use GCD to update the UI. – Ja͢ck Jul 08 '14 at 08:08
NSURL *videoURL = [NSURL fileURLWithPath:url];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Player autoplays audio on init
[player stop];
[player release];
Check this link for an alternative: thumbnailImageAtTime: now deprecated - What's the alternative?

- 1
- 1

- 3,579
- 1
- 27
- 37
-
@Maulik i tried above code to generate image its working fine,but i am not getting video time line how can i get this also do u have any idea – Bad Boy Jun 14 '12 at 05:45
-
the thumbnail image is not displaying the time. Can you please edit your code if you know this? – user1673099 Feb 19 '13 at 06:42
-
will I be able to use this for screen capturing the video and not for creating thumbnail? – SleepNot Apr 15 '13 at 04:14
-
I'm using this url http://www.sciencentral.com/news/image_db/2024515/NNM2212_AntPower_MSTR.mov I get nil thumbnail image. please help – ruyamonis346 Aug 07 '13 at 12:18
-
Isn't this approach much less memory friendly than the AVAssetImageGenerator approach? The way I understand it - the whole video needs to be loaded into memory in order to generate a single image. – Stavash Oct 31 '13 at 14:26
-
6thumbnailImageAtTime: is deprecated now, just FYI to anyone who visits this thread. – Chris C Jan 09 '15 at 15:41
SWIFT 2.0
You can generate in swift in two ways 1. AVFoundation 2. MPMoviePlayerController
1.
func generateThumnail(url : NSURL) -> UIImage{
var asset : AVAsset = AVAsset.assetWithURL(url) as AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
var error : NSError? = nil
var time : CMTime = CMTimeMake(1, 30)
var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
var frameImg : UIImage = UIImage(CGImage: img)!
return frameImg
}
2.
override func viewDidLoad() {
super.viewDidLoad()
var moviePlayer : MPMoviePlayerController! = MPMoviePlayerController(contentURL: moviePlayManager.movieURL)
moviePlayer.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width:
self.view.frame.size.width, height: self.view.frame.height)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.None
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "videoThumbnailIsAvailable:",
name: MPMoviePlayerThumbnailImageRequestDidFinishNotification,
object: nil)
let thumbnailTimes = 3.0
moviePlayer.requestThumbnailImagesAtTimes([thumbnailTimes],
timeOption: .NearestKeyFrame)
}
func videoThumbnailIsAvailable(notification: NSNotification){
if let player = moviePlayer{
let thumbnail =
notification.userInfo![MPMoviePlayerThumbnailImageKey] as? UIImage
if let image = thumbnail{
/* We got the thumbnail image. You can now use it here */
println("Thumbnail image = \(image)")
}
}

- 1
- 1

- 17,485
- 5
- 50
- 66
-
solution 1 works in most cases for me but not always do you know why ? http://stackoverflow.com/questions/37374008/ios-swift-video-thumbnail-error – Sam May 22 '16 at 11:58
-
1Actually i will take a tour on your post in free time. Thanks for your comment :) – Dharmbir Singh May 22 '16 at 12:11
Best method I've found... MPMoviePlayerController thumbnailImageAtTime:timeOption
-
1
-
2Actually on testing I find this method is much faster than the AVAsset code listed above. – GuybrushThreepwood Apr 21 '12 at 14:38
-
I'm using this url http://www.sciencentral.com/news/image_db/2024515/NNM2212_AntPower_MSTR.mov I get nil thumbnail image. please help – ruyamonis346 Aug 07 '13 at 12:21
-
1But in this method first video should play at once than only it gives thumbnail image of frame. So its better use AVURLAsset for getting thumbnail image of any video file. – Diken Shah Jan 20 '14 at 11:03
Swift 2 code:
func previewImageForLocalVideo(url:NSURL) -> UIImage?
{
let asset = AVAsset(URL: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
//If possible - take not the first frame (it could be completely black or white on camara's videos)
time.value = min(time.value, 2)
do {
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
return UIImage(CGImage: imageRef)
}
catch let error as NSError
{
print("Image generation failed with error \(error)")
return nil
}
}

- 16,927
- 4
- 52
- 72
-
It works in most cases but not always do you know why? http://stackoverflow.com/questions/37374008/ios-swift-video-thumbnail-error – Sam May 22 '16 at 11:56
For Swift 3.0
func createThumbnailOfVideoFromFileURL(_ strVideoURL: String) -> UIImage?{
let asset = AVAsset(url: URL(string: strVideoURL)!)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(Float64(1), 100)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
/* error handling here */
}
return nil }

- 2,963
- 1
- 25
- 21
For Swift 5
import AVKit
Code:
// Get Thumbnail Image from URL
fileprivate func getThumbnailFromUrl(_ url: String?, _ completion: @escaping ((_ image: UIImage?)->Void)) {
guard let url = URL(string: url ?? "") else { return }
DispatchQueue.main.async {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 2, timescale: 1)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
completion(thumbnail)
} catch {
print("Error :: ", error.localizedDescription)
completion(nil)
}
}
}
Usage :: Take one Image View
@IBOutlet weak var imgThumbnail: UIImageView!
Then after call getThumbnailFromUrl
method for thumbnail with URL String
as parameter
self.getThumbnailFromUrl(videoURL) { [weak self] (img) in
guard let _ = self else { return }
if let img = img {
self?.imgThumbnail.image = img
}
}
pls try this if it is use full then pls comment Thank you

- 505
- 5
- 15

- 162
- 1
- 11
-
I am using this solution is worked fine for me.Thanks Rohan Aryan – pansora abhay Sep 30 '20 at 04:57
Swift 4
func generateThumbnail(for asset:AVAsset) -> UIImage? {
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 1, timescale: 2)
let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)
if img != nil {
let frameImg = UIImage(cgImage: img!)
return frameImg
}
return nil
}
How to use:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true) {
switch mediaType {
case kUTTypeMovie:
guard info[UIImagePickerController.InfoKey.mediaType] != nil, let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL else { return }
let asset = AVAsset(url: url)
guard let img = self.generateThumbnail(for: asset) else {
print("Error: Thumbnail can be generated.")
return
}
print("Image Size: \(img.size)")
break
default:
break
}
}
}

- 1,377
- 19
- 32
Maybe this is useful for someone else who faces the same problem. I needed an easy solution for creating a thumbnail for Images, PDFs and Videos. To solve that problem I've created the following Library.
https://github.com/prine/ROThumbnailGenerator
The usage is very straightforward:
var thumbnailImage = ROThumbnail.getThumbnail(url)
It has internally three different implementations and depending on the file extension it does create the thumbnail. You can easily add your own implementation if you need a thumbnail creator for another file extension.

- 12,192
- 8
- 40
- 59
-
It works in most cases but not always do you know why? http://stackoverflow.com/questions/37374008/ios-swift-video-thumbnail-error – Sam May 22 '16 at 11:57
Swift 2.1 Getting thumbnails at required time intervals
func getPreviewImageForVideoAtURL(videoURL: NSURL, atInterval: Int) -> UIImage? {
print("Taking pic at \(atInterval) second")
let asset = AVAsset(URL: videoURL)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(Float64(atInterval), 100)
do {
let img = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
let frameImg = UIImage(CGImage: img)
return frameImg
} catch {
/* error handling here */
}
return nil
}

- 37
- 3
You will get Thumbnail Image From URL when you change "fileURLWithPath" to "URLWithString".
In My case it worked like this.
NSURL *url = [NSURL URLWithString:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;