【问题标题】:Create thumbnail image from video url!从视频网址创建缩略图!
【发布时间】:2011-01-29 04:09:27
【问题描述】:

我正在尝试从视频网址创建缩略图。 我使用 AV 基础编程指南。 我的项目有一个按钮和一个图像视图。按下按钮时,缩略图将加载到 uiimageview 上。 我的代码不能工作,它是:

- (IBAction) btnClick : (id)sender
{

    NSURL *url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=bgN62D70VLk"];

    AVURLAsset *myAsset = [[AVURLAsset alloc] initWithURL:url options:nil];

    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];

    Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);

    CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);

    NSError *error = nil;

    CMTime actualTime;

    CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

    if (halfWayImage != NULL) {

        NSString *actualTimeString = (NSString *)CMTimeCopyDescription(NULL, actualTime);

        NSString *requestedTimeString = (NSString *)CMTimeCopyDescription(NULL, midpoint);

        NSLog(@"got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);

        [actualTimeString release];

        [requestedTimeString release];

        // Do something interesting with the image.

        CGImageRelease(halfWayImage);

    }

    UIImage *image = [UIImage imageWithCGImage:halfWayImage];
    [imageView setImage:image];
    [imageGenerator release];

}

请帮我解决这个问题! 谢谢!

【问题讨论】:

    标签: iphone ios avfoundation


    【解决方案1】:

    MPMoviePlayerController 有办法处理这个问题 -

    thumbnailImageAtTime:timeOption:
    
    requestThumbnailImagesAtTimes: timeOption:
    
    cancelAllThumbnailImageRequests:
    

    【讨论】:

      【解决方案2】:

      我在您的代码中发现了一些问题:

      • http://www.youtube.com/watch?v=bgN62D70VLk 是网页的 URL,但 AVAsset 必须是视频或音频文件。注意:YouTube 不会宣传其视频文件的网址。
      • 您对 [myAsset duration] 的调用将被阻止。您应该改用 AVAsynchronousKeyValueLoading 协议(请参阅 loadValuesAsynchronouslyForKeys)。
      • 你在释放它后使用的是halfwayImage。

      我建议观看 WWDC 2010 的 AVFoundation 会议,并查看会议示例代码。

      【讨论】:

        【解决方案3】:

        这已经很晚了,但它会帮助其他提出这个问题的人。

        查看this回答同样的问题,希望对其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 2012-12-06
          • 2018-04-18
          • 2017-12-10
          • 2013-09-13
          • 2018-05-15
          • 1970-01-01
          • 2018-06-14
          • 2017-02-16
          • 1970-01-01
          相关资源
          最近更新 更多