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. JAVA中获取工程路径的方法

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  2. ZOJ 2587 Unique Attack (最小割唯一性)

    题意 判断一个无向图的割是否唯一 思路 错误思路:一开始想的是判断割边是否都是关键割边,那既然割边两端点能连通S.T点的边是关键边,那么只要遇到有某个边两端点不连通S or T则这条边就不是关键割边( ...

  3. angular依赖注入的理解(转)

    使用过java进行开发的人肯定知道大名鼎鼎的spring框架,对于spring的IOC肯定也有所了解,通过配置文件定义好bean之后,如果需要使用这些bean,不需要自己去实例化,而是跟spring这 ...

  4. chrome 远程调试(转)

    http://www.tuicool.com/articles/ZJfeAzi 由于 appspot.com被墙,一般调试不成功. 随着智能手机的普及,移动设备的浏览器功能越来越强大,我们用手机上网时 ...

  5. 动态加载so文件

    在开发过程中,经常会用到第三方库,比如地图.视频.文档编辑.图表之类.依赖这些库,需要添加其SDK,有时需要用到jni层的So文件,比如百度地图等. 那么问题来了,如果两个不同的库之间的so文件发生冲 ...

  6. Android02--debug.keystore的注册信息

    1 -- 签名文件的密钥 默认签名文件的密码是:android 该文件的存放点是: 2 -- 签名文件的签名信息 keytool -list -v -keystore C:\Users\motadou ...

  7. CXF之jaxws:endpoint对spring bean的引用

    由于CXF对spring的无缝支持,CXF的使用,经常与spring捆绑在一起.随之而起的,自然是想在jaxws:endpoint中引用spring bean.在CXF提供的HelloWorld例子中 ...

  8. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 _get_values_for_options(self, options)

    def _get_values_for_options(self, options): values = [] for option in options: values.append(option. ...

  9. Android引用百度定位API第三方组件后导致其它.so文件无法正常加载的问题

    查看当前调试设备CPU架构的方法: adb.exe shell getprop ro.product.cpu.abi  (一般返回值为:armeabi-v7a) adb.exe shell getpr ...

  10. C++第一章概述

    1:C++主要是对于C的继承性做的相当的出色,主要扩充在于程序员可以自己定义自己的数据结构,用数据结构去描述日常生活中的事务,而不是C语言中当当仅有的Struct数据类型等等 2: 每一种语言都有自己 ...