获取/制作来自videourl的缩略图,获取API响应

我收到来自服务器的响应中的videoURL(例如,来自不存储在本地设备中的API)的videoURL;

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>" ], 

目前它只包含YouTubevideo,但不能确定,它也可能包含来自其他资源(即YouTube以外)的urlvideo。

我从这个回应中提取了youtube urls。 并且想在UICollectionView中显示这些video的缩略图。

我怎样才能做到这一点?

我search了很多关于这个,但无法得到任何适当的解决scheme。

请帮帮我。

谢谢..

试试这个

 -(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); 

我已经使用下面的代码解决了我的问题。

所以我在这里附上代码供其他人参考;

 -(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:@";//(.+?)\\&quot;" 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; } 

检查此答案如何从YouTube API获取YouTubevideo缩略图?

每个YouTubevideo都有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 
 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];