我想在我视图的uiwebview框架中设置缩略图。 但是,当我使用以下代码时,虽然图像值为NOT NULL,但没有显示图像。有人可以帮忙吗,让我知道我哪里出错了?
请注意,在代码中,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();
答案 0 :(得分:-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=T6X3j7zxUHA
更改为http://www.youtube.com/embed/T6X3j7zxUHA
。
另一种不需要webview的方法就是手动构建缩略图。 (以下示例)
///////////////////////////////////////////////////////////////////////////////////////////////////
-(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;
}
现在您拥有了图片缩略图网址,只需抓取它(通过简单的GET
请求)。