美文网首页
iOS 获取本地视频的缩略图

iOS 获取本地视频的缩略图

作者: 杨继磊 | 来源:发表于2019-04-16 12:41 被阅读0次

    需要添加AVFoundation和CoreMedia.framework

     +(UIImage *)getImage:(NSString *)videoURL
    
    {
    
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
    
        AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
        gen.appliesPreferredTrackTransform = YES;
    
        CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    
        NSError *error = nil;
    
        CMTime actualTime;    
    
        CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
        UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    
        CGImageRelease(image);
    
        return thumb;
    
    
    }
    

    这个方法也可以

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; moviePlayer.shouldAutoplay = NO; 
    
    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
    
    //这个也一样
    
    
    +(UIImage *)fFirstVideoFrame:(NSString *)path
    {
        MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
                                       initWithContentURL:[NSURL fileURLWithPath:path]];
        UIImage *img = [mp thumbnailImageAtTime:0.0
                                     timeOption:MPMovieTimeOptionNearestKeyFrame];
        [mp stop];
        [mp release];
        return img;
    } 
    

    相关文章

      网友评论

          本文标题:iOS 获取本地视频的缩略图

          本文链接:https://www.haomeiwen.com/subject/nsxbwqtx.html