2012-04-27 67 views
0

我想在我的視圖的uiwebview框架中設置縮略圖圖像。 但是,當我使用下面的代碼時,雖然圖像值不是NULL,但沒有顯示圖像。有人可以幫忙,讓我知道我要去哪裏錯了嗎?無法爲UIWebView設置縮略圖圖像

請注意,在代碼中,VideoView類是從UIWebView派生而來的,其工作原理與此類似。

 video = [[VideoView alloc] 
           initWithStringAsURL:@"http://www.youtube.com/watch?v=T6X3j7zxUHA" 
           frame:CGRectMake(11, 7, 298, 311)]; 

    [self addSubview:video]; 

    UIGraphicsBeginImageContext(video.bounds.size); 
    [video.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *resultImageView = UIGraphicsGetImageFromCurrentImageContext(); 

    thumbnailButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [thumbnailButton setFrame:CGRectMake(0,0, 298, 311)]; 
    [thumbnailButton setImage:resultImageView forState:UIControlStateNormal]; 
    [video addSubview:thumbnailButton]; 
    UIGraphicsEndImageContext();  

回答

-1

我不會建議使用一個UIWebView只得到一個YouTube視頻的縮略圖,但這裏是一些代碼,我使用嵌入YouTube視頻在我的應用程序。

static NSString* EmbedHTML = "<html>\ 
<head>\ 
<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no, width=%0.0f\"/>\ 
</head>\ 
<iframe width=\"%0.0f\" height=\"%0.0f\" src=\"%@\" frameborder=\"0\" allowfullscreen> </iframe></html>"; 

使用嵌入視頻和改變你的YouTube網址從http://www.youtube.com/watch?v=T6X3j7zxUHAhttp://www.youtube.com/embed/T6X3j7zxUHA

另一種不需要web視圖的方法是手動構建縮略圖。 (下面的例子)

/////////////////////////////////////////////////////////////////////////////////////////////////// 
-(NSString*)youtubeThumb:(NSString*)url 
{ 
    NSRange end = [url rangeOfString:@"?"]; 
    if(end.location != NSNotFound) 
    { 
     NSRange start = [url rangeOfString:@"/" options:NSBackwardsSearch range:NSMakeRange(0, end.location)]; 
     if(start.location != NSNotFound) 
     { 
      NSString* video_id = [url substringWithRange:NSMakeRange(start.location+1, (end.location-1)-start.location)]; 
      return [NSString stringWithFormat:@"http://i.ytimg.com/vi/%@/2.jpg",video_id]; 
     } 
    } 
    return nil; 
} 

現在你有圖像縮略圖URL,只是把它拿來(通過一個簡單的GET要求)。

+0

謝謝daltoniam,我現在就試試這個...... – user1357849 2012-04-27 18:21:26

+0

不錯的交易。如果它最終爲你工作,請接受我的答案。如果您對代碼有任何疑問,請告訴我。 – daltoniam 2012-04-27 18:58:14

+0

嗨達爾頓,你提供的第二個解決方案爲我工作了幾個小小的修改。然而thumnail的大小是128x96。如果我想要更大尺寸的縮略圖,我該怎麼辦?有沒有辦法獲得自定義大小的縮略圖? – user1357849 2012-04-29 02:36:14