2011-01-29 119 views
0

我嘗試從視頻網址創建縮略圖圖像。 我使用AV Foundation編程指南。 我的項目有一個按鈕和一個imageview。按下按鈕後,縮略圖圖像將在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]; 

} 

幫助我這個問題,拜託了! 謝謝!

回答

0

MPMoviePlayerController有一些方法來處理這個問題 -

thumbnailImageAtTime:timeOption: 

requestThumbnailImagesAtTimes: timeOption: 

cancelAllThumbnailImageRequests: 
0

我看到你的代碼的一些問題:

  • http://www.youtube.com/watch?v=bgN62D70VLk是一個網頁的URL,但AVAssets必須是視頻或音頻文件。注意:YouTube不會公佈其視頻文件的網址。
  • 您對[myAsset持續時間]的呼叫將被阻止。您應該改用AVAsynchronousKeyValueLoading協議(請參閱loadValuesAsynchronouslyForKeys)。
  • 您在釋放它之後正在使用halfwayImage。

我會推薦觀看WWDC 2010的AVFoundation會議,並查看會話示例代碼。

0

這是很晚了,但它會幫助其他誰來談這個問題。

調查this回答同樣的問題,希望能對其他人有所幫助。