我在服务器的响应中获取视频URL(即来自未存储在本地设备中的API),如下所示;
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以外)的网址视频。
我从此回复中提取了youtube网址。并希望在UICollectionView中显示这些视频的缩略图。
我该怎么做?
我对此进行了大量搜索但未能得到任何适当的解决方案。
请帮帮我。
谢谢..
答案 0 :(得分:1)
尝试这个
-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}
尝试以下
AVAsset *asset = [AVAsset assetWithURL:sourceURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
答案 1 :(得分:1)
我使用以下代码解决了我的问题。
所以我在这里附上代码供其他人参考;
-(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;
}
答案 2 :(得分:1)
检查此答案How do I get a YouTube video thumbnail from the YouTube API?
每个YouTube视频都有4个生成的图片。它们的格式如下:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
列表中的第一个是全尺寸图像,其他是缩略图图像。默认缩略图图像(即1.jpg,2.jpg,3.jpg之一)是:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
答案 3 :(得分:0)
NSURL *videoURL = [NSURL fileURLWithPath:url];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Stop player
[player stop];
[player release];