iOS视频播放列表中默认展示图如何获取
2017-08-02 15:42
381 查看
1.让后台直接返回一个图片的URL地址,直接展示。这种方法是用的比较多的,比较通用的方法。
2.根据后台传过来的视频URL,iOS端自己截取视频的某一帧做为展示图。(此方法耗时比较严重,界面会卡顿)。
在此,单独说说第二种方法。
获取视频某一帧的代码,如下:
如果视频列表比较多,直接使用这个方法,界面肯定会卡,这时需要放到异步线程去处理,获得图片后再回到主线程更新界面。大致方法如下:
我这里将请求得到的图片存到了videoPreImageViewArray数组中,在tableView的代理方法中处理一下,当请求到图片之后再进行展示
以上就是大概的处理方法,虽然加到了异步线程,获取视频中某一帧还是有些慢,但是,不会出现卡顿了。 如果大家有更好的处理方法,欢迎沟通交流。
2.根据后台传过来的视频URL,iOS端自己截取视频的某一帧做为展示图。(此方法耗时比较严重,界面会卡顿)。
在此,单独说说第二种方法。
获取视频某一帧的代码,如下:
// 获取视频某一帧,返回UIImage - (UIImage*) getVideoPreViewImage:(NSURL *)path { AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil]; AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset]; assetGen.appliesPreferredTrackTransform = YES; //视图时间,想要截取对应时间的帧,可以更改这个参数 CMTime time = CMTimeMakeWithSeconds(2.0, 600); NSError *error = nil; CMTime actualTime;//可不设置,传NULL CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *videoImage = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return videoImage; }
如果视频列表比较多,直接使用这个方法,界面肯定会卡,这时需要放到异步线程去处理,获得图片后再回到主线程更新界面。大致方法如下:
__weak typeof(self) weakSelf = self; //异步请求 dispatch_async(dispatch_get_global_queue(0,0), ^{ for (int i = 0; i < 10; i++) { UIImage * image = [weakSelf getVideoPreViewImage:[NSURL URLWithString:videoUrl]]; if (image != nil) { [weakSelf.videoPreImageViewArray addObject:image]; //更新UI,回到主线程 dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } } });
我这里将请求得到的图片存到了videoPreImageViewArray数组中,在tableView的代理方法中处理一下,当请求到图片之后再进行展示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentify = @"VieoShowCell"; VieoShowCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"VieoShowCell" owner:self options:nil] lastObject]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; //重点看这里 if (self.videoPreImageViewArray.count > indexPath.row) { cell.imageVedioView.image = self.videoPreImageViewArray[indexPath.row]; } return cell; }
以上就是大概的处理方法,虽然加到了异步线程,获取视频中某一帧还是有些慢,但是,不会出现卡顿了。 如果大家有更好的处理方法,欢迎沟通交流。
相关文章推荐
- iOS 播放视频进行滑动获取每一帧图片并且通过UIImageView展示出来
- ios上微信视频不能自动播放和禁止默认全屏
- ios开发 如何在应用内获取当前周围wifi列表和强度 并实现在应用内控制wifi开关
- iOS开发——获取UIWebView中视频的长度与播放进度等信息
- iOS 获取本地视频播放路径
- 请教:Android正播放视频时的解码输出流如何获取?
- 写了个html5播放视频的video控件,只支持mp4和3gp(android和ios默认支持的格式就写了这个)
- iOS-获取视频长度和当前播放时间
- iOS--JSON解析后如何获取数据,并且展示到相应cell上
- iOS开发:获取一个视频、音频文件的播放时长
- 如何获取UIWebView中全屏播放视频事件
- 如何让音频跟视频在ios跟android上自动播放
- iOS 获取本地视频播放路径
- [iOS]列表视频播放
- C#如何获取本地的打印机列表并且指定默认打印机
- iOS-获取视频长度和当前播放时间
- 在iOS开发中如何播放视频
- iOS - AVPlayer播放视频,获取视频尺寸(宽高)
- iOS开发——获取UIWebView中视频的长度与播放进度等信息
- IOS移动端如何获取ArcGIS Server的服务列表