Here is how to create thumbnail for videos. Two steps:

  1. Use AVURLAsset to access to the video file by URL. Then use AVAssetImageGenerator to generate an image of one frame.
  2. To distinguish the image we generated from normal images, add a play icon on the top of the image by merging two UIImage .

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
+ (UIImage *)createVideoThumbnailWithPath:(NSString *)videoPath {
if (!videoPath)
return nil;

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoPath] options:nil];
AVAssetImageGenerator *imgGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imgGenerator.appliesPreferredTrackTransform = YES;
imgGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

// create CGImage
CGImageRef thumbnailImgRef = nil;
// the time frame
CFTimeInterval thumbnailImageTime = 0;
NSError *error = nil;

// get the image
thumbnailImgRef = [imgGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:nil error:&error];

// error handling
if (!thumbnailImgRef)
NSLog(@"VIDEO THUMBNAIL ERROR: %@", error);

// convert to UIImage
UIImage *image = thumbnailImgRef ? [[UIImage alloc] initWithCGImage:thumbnailImgRef] : nil;

// add playIcon
if (image) {
// grab your play icon
UIImage *playIcon = [UIImage imageNamed:@"PlayIcon"];

// start image context
UIGraphicsBeginImageContext(image.size);

// add original image first with existing opacity
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

// add play icon over the image in the middle with supplied opacity if applicable
[playIcon drawInRect:CGRectMake((image.size.width - playIcon.size.width) / 2.0,
(image.size.height - playIcon.size.height) / 2.0,
playIcon.size.width,playIcon.size.height)
blendMode:kCGBlendModeNormal
alpha:0.9];

// get merged image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
}
return image;
}