A.需求
  • 所有数据都从服务器下载
  • 动画列表包含:图片、动画名标题、时长副标题
  • 点击打开动画观看
 
 
B.实现
1.显示图片和基本信息
 
服务器端的json信息:
{ "videos": [
{

  "name":"驯龙高手1",
  "length":"16秒",
  "image":"images/[20150124-180852-0].PNG",
  "video":"videos/1.MP4"
},
{
  "name":"驯龙高手2",
  "length":"21秒",
  "image":"images/[20150124-180905-1].PNG",
  "video":"videos/2.MP4"
},
{
  "name":"驯龙高手3",
  "length":"14秒",
  "image":"images/[20150124-180908-2].PNG",
  "video":"videos/3.MP4"
},
...
]
}
 
 
(1)使用tableView来布置界面
 
(2)创建一个模型,用来装载录像信息
 //
// HVWVideo.h
// VideoOnlineDemo
//
// Created by hellovoidworld on 15/1/24.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWVideo : NSObject @property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *length;
@property(nonatomic, strong) NSString *image;
@property(nonatomic, strong) NSString *video; + (instancetype) videoWithDict:(NSDictionary *) dict; @end
 
 
(3)发送请求 & 接收服务器信息
  • 使用代理方法发送请求,接收json数据
  • 解析json数据,封装到模型中
  • 使用SDWebImage框架加载服务器图片
 //
// HVWVideoViewController.m
// VideoOnlineDemo
//
// Created by hellovoidworld on 15/1/25.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWVideoViewController.h"
#import "HVWVideo.h"
#import "UIImageView+WebCache.h" #define ServerIP @"http://192.168.0.21:8080/MyTestServer" @interface HVWVideoViewController () <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate> /** 接收到的二进制数据 */
@property(nonatomic, strong) NSMutableData *data; /** 所有的录像模型数据 */
@property(nonatomic, strong) NSArray *videos; @end @implementation HVWVideoViewController - (void)viewDidLoad {
[super viewDidLoad]; self.tableView.dataSource = self;
self.tableView.delegate = self; // 读取服务器数据
[self acceptDataFromServer];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /** 读取服务器数据 */
- (void) acceptDataFromServer {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/video", ServerIP]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
} #pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.videos.count;
} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"VideoCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
} HVWVideo *video = self.videos[indexPath.row];
// 设置标题 & 副标题
cell.textLabel.text = video.name;
cell.detailTextLabel.text = video.length; // 下载图片
NSString *imageUrlStr = [NSString stringWithFormat:@"%@/%@", ServerIP, video.image];
NSURL *imageUrl = [NSURL URLWithString:imageUrlStr];
[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"placeholder"]]; // 下载录像 return cell;
} #pragma mark - UITableViewDelegate #pragma mark - NSURLConnectionDataDelegate 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"开始接收数据");
self.data = [NSMutableData data];
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"接收数据中...");
[self.data appendData:data];
} - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"接收数据完毕"); if (self.data) {
// 加载录像资料
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableLeaves error:nil]; // NSLog(@"%@", jsonDict);
NSArray *jsonArray = jsonDict[@"videos"];
// NSLog(@"%@", jsonArray); NSMutableArray *videoArray = [NSMutableArray array];
for (NSDictionary *dict in jsonArray) {
NSLog(@"%@", dict);
HVWVideo *video = [HVWVideo videoWithDict:dict];
[videoArray addObject:video];
}
self.videos = videoArray; [self.tableView reloadData];
} } @end
 
(4)调整cell高度,创建自定义cell类,调整尺寸、分割线

 //
// HVWVideoCell.m
// VideoOnlineDemo
//
// Created by hellovoidworld on 15/1/25.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWVideoCell.h"
#import "UIImageView+WebCache.h" #define ServerIP @"http://192.168.0.21:8080/MyTestServer" @interface HVWVideoCell() /** 分割线 */
@property(nonatomic, strong) UIView *separatorLine; @end @implementation HVWVideoCell + (instancetype) cellWithTableView:(UITableView *) tableView {
static NSString *ID = @"VideoCell"; HVWVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (nil == cell) {
cell = [[HVWVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} return cell;
} - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 重写样式
// 自定义分割写
UIView *separatorLine = [[UIView alloc] init];
separatorLine.backgroundColor = [UIColor lightGrayColor];
separatorLine.alpha = 0.3;
[self.contentView addSubview:separatorLine]; self.separatorLine = separatorLine;
} return self;
} /** 重写内部子控件的位置尺寸 */
- (void)layoutSubviews {
[super layoutSubviews]; // 设置图片
CGFloat imageWidth = ;
CGFloat imageHeight = ;
CGFloat imageX = ;
CGFloat imageY = (self.frame.size.height - imageHeight) / ;
CGRect imageFrame = CGRectMake(imageX, imageY, imageWidth, imageHeight);
self.imageView.frame = imageFrame; // 设置标题
CGRect textFrame = self.textLabel.frame;
textFrame.origin.x = imageX + imageWidth + ;
self.textLabel.frame = textFrame; // 设置副标题
CGRect detailFrame = self.detailTextLabel.frame;
detailFrame.origin.x = self.textLabel.frame.origin.x;
self.detailTextLabel.frame = detailFrame; // 设置分割线
CGFloat separatorLineY = self.frame.size.height - ;
self.separatorLine.frame = CGRectMake(, separatorLineY, self.frame.size.width, );
} /** 设置数据 */
- (void)setVideo:(HVWVideo *)video {
_video = video; // 设置标题 & 副标题
self.textLabel.text = video.name;
self.detailTextLabel.text = video.length; // 下载图片
NSString *imageUrlStr = [NSString stringWithFormat:@"%@/%@", ServerIP, video.image];
NSURL *imageUrl = [NSURL URLWithString:imageUrlStr];
[self.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"placeholder"]];
} @end
 
2.播放视频
 
(1)引入MediaPlayer类
 #import <MediaPlayer/MediaPlayer.h>
 
(2)使用MPMoviePlayerViewController 播放视频
 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 加载视频
HVWVideo *video = self.videos[indexPath.row];
NSString *videoUrlStr = [NSString stringWithFormat:@"%@/%@", ServerIP, video.video];
NSURL *videoUrl = [NSURL URLWithString:videoUrlStr]; NSLog(@"%@", videoUrlStr); // 使用MediaPlayer框架播放视频
MPMoviePlayerViewController *mvController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];
[self presentMoviePlayerViewControllerAnimated:mvController];
}
 
(3)阻止app进入后台之后退出视频
a.默认app进入后台之后,会自动把视频播放的view缩回去
 
b.自定义一个集成MPMoviePlayerViewController的类,通过取消接收“进入后台”消息来阻止此操作
 //
// HVWMoviePlayerViewController.m
// VideoOnlineDemo
//
// Created by hellovoidworld on 15/1/25.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWMoviePlayerViewController.h" @interface HVWMoviePlayerViewController () @end @implementation HVWMoviePlayerViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // 取消接收程序进入后台的通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
} /** 重写屏幕方向方法 */
- (NSUInteger)supportedInterfaceOrientations {
// 横屏,向左/右
return UIInterfaceOrientationMaskLandscape;
} @end
 
 
 
 
(4)强制使用横屏播放
(默认是3个方向都可以播放)
 /** 重写屏幕方向方法 */
- (NSUInteger)supportedInterfaceOrientations {
// 横屏,向左/右
return UIInterfaceOrientationMaskLandscape;
}
 
 
 
 
 
 
 
 

[iOS 多线程 & 网络 - 3.0] - 在线动画Demo的更多相关文章

  1. [iOS 多线程 & 网络 - 2.0] - 发送接收 服务器信息

    A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器 1.准备好合适版本的JDK.eclipse EE.tomcat.struts2 框架包 2.配置JDK ...

  2. [iOS 多线程 & 网络 - 1.0] - 多线程概述

    A.进程 什么是进程进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过"活 ...

  3. [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用

    A.AFN基本知识 1.概念 AFNetworking 是对NSURLConnection的封装 运行效率没有ASI高(因为ASI基于CFNetwork),但是使用简单 AFN支持ARC     B. ...

  4. [iOS 多线程 & 网络 - 2.9] - ASI框架

    A.ASI基本知识 1.ASI简单介绍 ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大. ASI的实现基于底层的CFNetwork框架,因此运行效率很高. ASI的g ...

  5. [iOS 多线程 & 网络 - 2.3] - 解析xml

    A.XML基本知识 1.xml概念 什么是XML全称是Extensible Markup Language,译作“可扩展标记语言”跟JSON一样,也是常用的一种用于交互的数据格式一般也叫XML文档(X ...

  6. [iOS 多线程 & 网络 - 2.1] - 解析json

    A.iOS中json的基本使用 1.解析json数据 (1)json反序列化 对象{}格式 {key : value, key : value,...} 的键值对的结构可以反序列化为OC中的NSDic ...

  7. [iOS 多线程 & 网络 - 1.1] - 多线程NSThread

    A.NSThread的基本使用 1.创建和启动线程 一个NSThread对象就代表一条线程创建.启动线程NSThread *thread = [[NSThread alloc] initWithTar ...

  8. 【重点突破】—— Vue2.0 transition 动画Demo实践填坑

    前言:vue1.0版本和2.0版本的过渡系统改变是很大的,具体请详看文档介绍.本文转载自郭锦荣的博客,一共列举了四种transition的使用实践,分别是css过渡.css动画.javascript钩 ...

  9. [iOS 多线程 & 网络 - 2.11] - ASI框架上传文件

    A.ASI的上传功能基本使用 1.实现步骤 (1)创建请求 使用ASIFormDataRequest (2)设置上传文件路径 (3)发送请求     2.上传相册相片 UIImagePickerCon ...

随机推荐

  1. C++ STL之查找算法

    C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...

  2. JS中令人发指的valueOf方法介绍

    彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.prototype.valueOf ( ) When the ...

  3. hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

    题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 最后的差确定,只需要计算和的种类 ...

  4. JDBC 事务控制

    一.简介: 前面一遍提到了jdbc事务相关的概念.从中了解到事务应具有ACID特性.所以对于javaweb开发来说,某一个service层的方法,应该是一个事务,应该是具有原子性的.特别是当一个ser ...

  5. 宏buf_pool_t

    typedef struct buf_pool_struct buf_pool_t; struct buf_pool_struct{ /** @name General fields */ /* @{ ...

  6. 使用截图方式将Excel导出为PNG图片的不可行性

    博主前面一篇文章使用了JAVA的Robot机制 模拟打开Excel然后Robot移动到指定区域,截图并生成PNG格式图片 试图使用这种方式将复杂的Excel报表转化成无差别的PNG图片 但是这种方式遇 ...

  7. Alpha、Beta、RC、GA版本的区别

    Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用. Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出. RC:(Release Candi ...

  8. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...

  9. Eclipse小技巧<一>

    Eclipse是一款特别好用的开源开发工具,基于插件的特性使其能够进行各种语言的开发.非常喜欢eclipse里的编码风格,感觉这个开发工具十分灵活,又有很多开发的小技巧能够提高开发效率,每次学到一个t ...

  10. 云计算服务模型,第 1 部分: 基础架构即服务(IaaS)

    英文原文:Cloud computing service models, Part 1: Infrastructure as a Service 本文介绍三个云类别中的第一个:基础架构即服务(infr ...