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. Android开发之三种动画

    转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...

  2. [IE编程] 多页面基于IE内核浏览器的代码示例

    有不少人发信问这个问题,我把答案贴在这里: 建议参考 WTL (Windows Template Library) 的代码示例工程TabBrowser  (在WTL目录/Samples/TabBrow ...

  3. 12 Useful “df” Commands to Check Disk Space in Linux

    On the internet you will find plenty of tools for checking disk space utilization in Linux. However, ...

  4. .frm文件

    http://www.cnblogs.com/jiangxu67/p/4755097.html MySQL]frm文件解析 MySQL 协议字节序 关于传输整数小大端的实现 http://hamilt ...

  5. 基于XMPP的即时通信系统的建立(二)— XMPP详解

    XMPP详解 XMPP(eXtensible Messaging and Presence Protocol,可扩展消息处理和现场协议)是一种在两个地点间传递小型结构化数据的协议.在此基础上,XMPP ...

  6. UrlRewriter.dll伪静态实现二级域名泛解析

    大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写, 如:可将 http://http://www.115sou.com/qq/  ...

  7. UVALive 5713 Qin Shi Huang's National Road System(次小生成树)

    题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...

  8. apache开源项目-- UIMA

    UIMA (Unstructured Information Management applications) 是一个软件系统,用来分析大量的非结构化信息从而发掘中对最终用户有用的知识点,一个最典型的 ...

  9. DataGuard相同SID物理Standby搭建

    Oracle Data Guard 是针对企业数据库的最有效和最全面的数据可用性.数据保护和灾难恢复解决方案.它提供管理.监视和自动化软件基础架构来创建和维护一个或多个同步备用数据库,从而保护数据不受 ...

  10. Zend Framework 入门(3)—错误处理

    undefined 说明脚本中一些对象没有设定 你应在在使用该对象前进行判断 例如: if(typeof(aobject) == "undefined"){   //错误处理 }