Is it possible to get the first image frame of a video and display it in uiimageview . My video is saved in the server. i need to call the url to play the video
Asked
Active
Viewed 4,065 times
3 Answers
3
An example:
NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
[YourImageView setImage:img];
Hope it helps..

lakshmen
- 28,346
- 66
- 178
- 276
-
1the question explicitly says that the video is on the server (remote).. thus fileURLWithPath which are used for filesystem url are not correct – Heavy_Bullets Aug 24 '13 at 04:50
-
Hi lakesh, Actually when i scroll the Table view then the table view hangs & after some time they scroll..... – Gurpreet Feb 13 '14 at 07:14
-
@Gurpreet My view stucks, when I use such method. – Arpit B Parekh Mar 17 '17 at 12:13
-
http://stackoverflow.com/questions/37343358/how-to-generate-the-thumbnail-of-video-url-flv-format?rq=1 this worked for me. Just I have to call the method after some time interval so UI does not freeze. – Arpit B Parekh Mar 17 '17 at 13:13
3
I use this method to do the same
/**
* This method retunrs a thumbnail of a Video file
*/
+ (UIImage *)generateThumbnailIconForVideoFileWith:(NSURL *)contentURL WithSize:(CGSize)size
{
UIImage *theImage = nil;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.maximumSize=size;
generator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(100,100); //change whatever you want here.
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
CGImageRelease(imgRef);
return theImage;
}

Satheesh
- 10,998
- 6
- 50
- 93
0
You can do this using MPMoviePlayerController
like bellow..
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:yourVideoURL];
UIImage *videoThumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[player stop];
Also see another answer with AVURLAsset
from this link getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk

Community
- 1
- 1

Paras Joshi
- 20,427
- 11
- 57
- 70