I want to display a videothumbnail image of a video from url in my uiimageview for iphone

安稳与你 提交于 2019-12-06 09:05:12

问题


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


回答1:


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..




回答2:


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;
}



回答3:


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



来源:https://stackoverflow.com/questions/17962473/i-want-to-display-a-videothumbnail-image-of-a-video-from-url-in-my-uiimageview-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!