获取/制作来自视频网址的缩略图,以获得API响应 [英] Get / Make thumbnail image from video url getting in API response
问题描述
我在服务器的响应中获取视频网址(即来自未存储在本地设备中的API),如下所示;
I am getting video URLs in the response from server (i.e from API not stored in local-device) as follwos ;
videos": [
"<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/-OB7y6ELDks?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>",
"<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/igcIaNi-eHA?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>",
"<iframe width=\\"560\\" height=\\"315\\" src=\\"//www.youtube.com/embed/3kGNMUfajJc?list=UUYv2cbGZE2SmAYPDZQRKiig\\" frameborder=\\"0\\" allowfullscreen></iframe>"
],
目前它只包含youtube视频,但不确定,它还可能包含来自其他资源的网址视频(即除youtube之外的其他资源)。
Currently it contains only youtube videos, but it is not sure, it may also contain url videos from other resources (i.e other than youtube).
我从此响应中提取了你的网址并希望在UICollectionView中显示这些视频的缩略图。
I extracted youtube urls from this response. And want to display thumbnails of these videos in the UICollectionView.
我该怎么办?
I对此进行了大量搜索,但无法得到任何正确的解决方案。
I searched a lot about this but could not get any proper solution.
请帮帮我。
谢谢..
推荐答案
我使用以下代码解决了我的问题。
I have solved my problem using following code.
所以我在这里附加代码供其他人参考;
So I am attaching code here for others reference ;
-(void)setImage:(NSString*)urlResponse
{
NSString *youtubeUrl = [NSString stringWithFormat:@"%@",[self parseVideoHTMLUrl: urlResponse]];
NSString *videoThumbUrl = [self getYoutubeVideoThumbnail: youtubeUrl];
NSURL* videoURL =[NSURL URLWithString:videoThumbUrl];
[btn_photo sd_setImageWithURL:videoURL forState:UIControlStateNormal];
}
-(NSString *)parseVideoHTMLUrl:(NSString *)videoUrl
{
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@";//(.+?)\\""
options:NSRegularExpressionCaseInsensitive
error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:videoUrl options:0 range:NSMakeRange(0, videoUrl.length)];
NSString *url = [videoUrl substringWithRange:[textCheckingResult rangeAtIndex:1]];
return url;
}
-(NSString*)getYoutubeVideoThumbnail:(NSString*)youTubeUrl
{
NSString* video_id = @"";
if (youTubeUrl.length > 0)
{
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youTubeUrl
options:0
range:NSMakeRange(0, [youTubeUrl length])];
if (match)
{
NSRange videoIDRange = [match rangeAtIndex:0];
video_id = [youTubeUrl substringWithRange:videoIDRange];
NSLog(@"%@",video_id);
}
}
NSString* thumbImageUrl = [NSString stringWithFormat:@"http://img.youtube.com/vi/%@/default.jpg",video_id];
return thumbImageUrl;
}
这篇关于获取/制作来自视频网址的缩略图,以获得API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!