博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型
阅读量:4330 次
发布时间:2019-06-06

本文共 3567 字,大约阅读时间需要 11 分钟。

********HMVideosViewController.m

#import "HMVideosViewController.h"#import "MBProgressHUD+MJ.h"#import "HMVideo.h"#import "UIImageView+WebCache.h"#import 
#define HMUrl(path) [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@", path]]@interface HMVideosViewController ()@property (nonatomic, strong) NSMutableArray *videos;@end@implementation HMVideosViewController- (NSMutableArray *)videos{ if (!_videos) { self.videos = [[NSMutableArray alloc] init]; } return _videos;}- (void)viewDidLoad{ [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl(@"video"); // 2.创建请求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError || data == nil) { [MBProgressHUD showError:@"网络繁忙,请稍后再试!"]; return; } // 解析JSON数据 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSArray *videoArray = dict[@"videos"]; for (NSDictionary *videoDict in videoArray) { HMVideo *video = [HMVideo videoWithDict:videoDict]; [self.videos addObject:video]; } // 刷新表格 [self.tableView reloadData]; }];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.videos.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"video"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } HMVideo *video = self.videos[indexPath.row]; cell.textLabel.text = video.name; cell.detailTextLabel.text = [NSString stringWithFormat:@"时长 : %d 分钟", video.length]; // 显示视频截图 NSURL *url = HMUrl(video.image); [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placehoder"]]; return cell;}#pragma mark - 代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.取出对应的视频模型 HMVideo *video = self.videos[indexPath.row]; // 2.创建系统自带的视频播放控制器 NSURL *url = HMUrl(video.url); MPMoviePlayerViewController *playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; // 3.显示播放器 [self presentViewController:playerVc animated:YES completion:nil];}@end

************HMVideosViewController.h

#import 
@interface HMVideosViewController : UITableViewController@end

***********模型m

#import "HMVideo.h"@implementation HMVideo+ (instancetype)videoWithDict:(NSDictionary *)dict{    HMVideo *video = [[self alloc] init];    [video setValuesForKeysWithDictionary:dict];    return video;}@end

***********模型h

#import 
@interface HMVideo : NSObject/** * ID */@property (nonatomic, assign) int id;/** * 时长 */@property (nonatomic, assign) int length;/** * 图片(视频截图) */@property (nonatomic, copy) NSString *image;/** * 视频名字 */@property (nonatomic, copy) NSString *name;/** * 视频的播放路径 */@property (nonatomic, copy) NSString *url;+ (instancetype)videoWithDict:(NSDictionary *)dict;@end

 

转载于:https://www.cnblogs.com/ios-g/p/4807483.html

你可能感兴趣的文章
main(argc,argv[])
查看>>
在线教育工具—白板系统的迭代1——bug监控排查
查看>>
121. Best Time to Buy and Sell Stock
查看>>
hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
查看>>
安装php扩展
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>