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>"
],
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).
I extracted youtube urls from this response. And want to display thumbnails of these videos in the UICollectionView.
How can i do this?
I searched a lot about this but could not get any proper solution.
Please help me.
Thanks..
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;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments