【问题标题】:I want to display a videothumbnail image of a video from url in my uiimageview for iphone我想在我的 uiimageview for iphone 中显示来自 url 的视频的 videothumbnail 图像
【发布时间】:2015-07-05 21:23:17
【问题描述】:

是否可以获取视频的第一个图像帧并将其显示在 uiimageview 中。我的视频保存在服务器中。我需要调用 url 来播放视频

【问题讨论】:

    标签: iphone ios video-streaming video-thumbnails


    【解决方案1】:

    一个例子:

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

    希望对你有帮助..

    【讨论】:

    • 问题明确表示视频在服务器(远程)上。因此用于文件系统 url 的 fileURLWithPath 不正确
    • 嗨,lakesh,实际上,当我滚动表格视图时,表格视图会挂起,一段时间后它们会滚动.....
    • @Gurpreet 当我使用这种方法时,我的观点卡住了。
    • stackoverflow.com/questions/37343358/… 这对我有用。只是我必须在一段时间后调用该方法,这样 UI 就不会冻结。
    【解决方案2】:

    我用这个方法做同样的事情

    /**
     *  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】:

      您可以使用MPMoviePlayerController 来执行此操作,如下所示..

      MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:yourVideoURL];
      UIImage *videoThumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
      [player stop];
      

      还可以通过此链接getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk 看到AVURLAsset 的另一个答案

      【讨论】:

        猜你喜欢
        • 2014-05-05
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-24
        • 1970-01-01
        • 2012-07-14
        相关资源
        最近更新 更多