Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法为UIWebView设置缩略图图像_Uiwebview_Video Thumbnails - Fatal编程技术网

无法为UIWebView设置缩略图图像

无法为UIWebView设置缩略图图像,uiwebview,video-thumbnails,Uiwebview,Video Thumbnails,我想在视图的uiwebview框架中设置缩略图。 但是,当我使用以下代码时,没有显示图像,尽管图像值不是NULL。有人能帮忙让我知道我哪里出了问题吗 请注意,在代码中,VideoView类派生自UIWebView,其工作原理与UIWebView相同 video = [[VideoView alloc] initWithStringAsURL:@"http://www.youtube.com/watch?v=T6X3j7

我想在视图的uiwebview框架中设置缩略图。 但是,当我使用以下代码时,没有显示图像,尽管图像值不是NULL。有人能帮忙让我知道我哪里出了问题吗

请注意,在代码中,VideoView类派生自UIWebView,其工作原理与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();    

我不建议使用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>";

现在您有了图像缩略图URL,只需获取它(通过一个简单的
GET
请求)。

很好。如果它最终对你有用,请接受我的回答。另外,如果您对代码有任何疑问,请告诉我。您好,daltoniam,您提供的第二个解决方案对我有用,只做了一些小修改。然而,tht thumnail的尺寸是128x96。如果我想要更大尺寸的缩略图,我该怎么办?有没有办法获得自定义大小的缩略图?有两种方法可以通过查看此url来调整uiimage的大小。您还可以通过请求0.jpg而不是2.jpg来获取较大的主缩略图
  ///////////////////////////////////////////////////////////////////////////////////////////////////
-(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;
}