AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中
1.下载文件 和进度条处理代码
- (void)timer:(NSTimer *)timer{
// 另一个View中 进度条progress属性赋值
_downloadView.progress = self.pressing;
if (self.pressing >= 1.0) {
[timer invalidate];
}
}
-(void)downloadWithUrlString:(NSString *)urlString
{
//1.创建会话管理者
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
// NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__weak typeof(self)weakself = self;
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//监听下载进度
//completedUnitCount 已经下载的数据大小
//totalUnitCount 文件数据的中大小
// _downloadView.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
NSLog(@"%f",1.0 *downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
_pressing = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
NSRunLoop *mainLoop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.016 target:weakself selector:@selector(timer:) userInfo:nil repeats:NO];
// 添加到任务池中
[mainLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[mainLoop run];
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
/**
* 1:1:请求路径:NSUrl *url = [NSUrl urlWithString:path];从网络请求路径 2:把本地的file文件路径转成url,NSUrl *url = [NSURL fileURLWithPath:fullPath];
2:返回值是一个下载文件的路径
*
*/
// 在只定路径下 创建文件夹:fireFileDataSource
NSString *fullPathFileDataSource = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"fireFileDataSource"];
[weakself.fileToolManager createPath:fullPathFileDataSource];
// 下载文件存储的路径 拼接上文件的名字
NSString *fullPath = [NSString stringWithFormat:@"%@/%@",fullPathFileDataSource,response.suggestedFilename];
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
/**
*filePath:下载后文件的保存路径
*/
NSLog(@"%@",filePath);
}];
//3.执行Task
[download resume];
}
注意问题 如果只是定时器 不会走,需要添加到NSRunLoop中 才行 这个是一个坑 重要代码注意如下
- (void)timer:(NSTimer *)timer{
// 另一个View中 进度条progress属性赋值
_downloadView.progress = self.pressing;
if (self.pressing >= 1.0) {
[timer invalidate];
}
}
NSRunLoop *mainLoop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.016 target:weakself selector:@selector(timer:) userInfo:nil repeats:NO];
// 添加到任务池中
[mainLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[mainLoop run];
2.进度条代码 写在另一个View中 帖出如下
弹出view代码
_downloadView = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT)];
[_downloadView show];
(1)DownloadView.h
#import <UIKit/UIKit.h> @interface DownloadView : UIView @property(nonatomic,strong) UIProgressView *progressView;//进度条
@property(nonatomic,assign) CGFloat progress; - (void)show; @end
(2)DownloadView.m
#import "DownloadView.h" @interface DownloadView () @property(nonatomic,strong) UIView *backView; //后背景
@property(nonatomic,strong) UIView *bgView; //背景
@property(nonatomic,strong) UIView *topView; //上面
@property(nonatomic,strong) UIView *contentView;//内容
@property(nonatomic,strong) UILabel *titleLabel;//标题
@property(nonatomic,strong) UILabel *numberLabel;//数字 @property(nonatomic,strong) UIButton *closeButton;//关闭按钮
@property(nonatomic,strong) UIView *ttView; @end @implementation DownloadView - (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.alpha = ;
[self addSubview:self.backView];
[self.backView addSubview:self.bgView];
[self.backView addSubview:self.topView];
[self.backView addSubview:self.contentView];
[self.backView addSubview:self.titleLabel];
[self.backView addSubview:self.numberLabel];
[self.backView addSubview:self.progressView];
[self.backView addSubview:self.closeButton];
[self.backView addSubview:self.ttView]; __weak __typeof(self)weakSelf = self;
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo( / WIDTH_5S_SCALE);
make.height.mas_equalTo( / WIDTH_5S_SCALE);
make.center.equalTo(weakSelf);
}];
[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(weakSelf.bgView);
make.height.mas_equalTo( / WIDTH_5S_SCALE);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(weakSelf.topView);
make.centerY.equalTo(weakSelf.topView).offset(- / WIDTH_5S_SCALE);
//make.center.equalTo(weakSelf.topView);
}];
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo();
make.centerX.equalTo(weakSelf.bgView.mas_right).offset(- / WIDTH_5S_SCALE);
make.centerY.equalTo(weakSelf.bgView.mas_top).offset( / WIDTH_5S_SCALE);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.topView.mas_bottom);
make.left.equalTo(weakSelf.bgView.mas_left);
make.bottom.equalTo(weakSelf.bgView.mas_bottom);
make.right.equalTo(weakSelf.bgView.mas_right);
}];
[self.ttView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.topView.mas_bottom).offset(- / WIDTH_5S_SCALE);
make.left.equalTo(weakSelf.topView.mas_left);
make.right.equalTo(weakSelf.topView.mas_right);
make.height.mas_equalTo();
}];
[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.contentView.mas_top).offset( / WIDTH_5S_SCALE);
make.height.mas_equalTo( / WIDTH_5S_SCALE);
make.left.equalTo(weakSelf.contentView.mas_left);
make.right.equalTo(weakSelf.contentView.mas_right);
}];
[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.numberLabel.mas_bottom).offset( / WIDTH_5S_SCALE);
make.left.equalTo(weakSelf.contentView.mas_left).offset( / WIDTH_5S_SCALE);
make.right.equalTo(weakSelf.contentView.mas_right).offset(- / WIDTH_5S_SCALE);
make.height.mas_equalTo();
}]; }
return self;
} #pragma mark -- Event - (void)show{
[[UIApplication sharedApplication].delegate.window addSubview:self];
[UIView animateWithDuration:0.15 animations:^{
self.alpha = ;
}];
} - (void)closeBtnClick{
[UIView animateWithDuration:. animations:^{
self.alpha = ;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
} #pragma mark -- init - (UIView *)backView{
if (!_backView) {
_backView = [[UIView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT)];
_backView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
}
return _backView;
} - (UIView *)bgView{
if (!_bgView) {
_bgView = [[UIView alloc] init];
_bgView.backgroundColor = getColor(whiteColor);
_bgView.layer.cornerRadius = ;
_bgView.layer.masksToBounds = YES;
}
return _bgView;
} - (UIView *)topView{
if (!_topView) {
_topView = [[UIView alloc] init];
_topView.backgroundColor = getColor(mainColor);
_topView.layer.cornerRadius = ;
_topView.layer.masksToBounds = YES;
}
return _topView;
} - (UIView *)contentView{
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = getColor(whiteColor);
_contentView.layer.cornerRadius = ;
_contentView.layer.masksToBounds = YES;
}
return _contentView;
} - (UIView *)ttView{
if (!_ttView) {
_ttView = [[UIView alloc] init];
_ttView.backgroundColor = getColor(whiteColor);
}
return _ttView;
} - (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = DEF_FontSize_14;
_titleLabel.textColor = getColor(@"fa2671");
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.text = @"下载硬件程序";
}
return _titleLabel;
} - (UILabel *)numberLabel{
if (!_numberLabel) {
_numberLabel = [[UILabel alloc] init];
//_numberLabel.font = DEF_FontSize_26;
[_numberLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:]];
_numberLabel.textColor = getColor(bgColor);
_numberLabel.textAlignment = NSTextAlignmentCenter;
// _numberLabel.text = @"50%";
}
return _numberLabel;
} - (UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.progressTintColor = getColor(bgColor);//进度条颜色
_progressView.trackTintColor = getColor(@"d2d2d2");//默认也为灰色
//_progressView.progressImage = [UIImage imageNamed:@"icon_vedio_progress"];
//_progressView.trackImage = [UIImage imageNamed:@"icon_vedio_progressgrey"];
_progressView.progressViewStyle = UIProgressViewStyleDefault;
_progressView.layer.cornerRadius = ;
_progressView.layer.masksToBounds = YES;
[_progressView setProgress: animated:YES]; }
return _progressView;
} - (UIButton *)closeButton{
if (!_closeButton) {
_closeButton = [[UIButton alloc] init];
[_closeButton setBackgroundImage:[UIImage imageNamed:@"icon_vedio_close"] forState:UIControlStateNormal];
[_closeButton addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _closeButton;
} /*
- (void)sourceTimer{
dispatch_source_t _sourceTimer;
//创建全局队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建定时器
_sourceTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//定时器延时
NSTimeInterval delayTime = 1.0f;
//定时器时间间隔
NSTimeInterval timeInteral = 0.5f;
//设置开始时间
dispatch_time_t startDelayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC));
//设置定时器
dispatch_source_set_timer(_sourceTimer, startDelayTime, timeInteral*NSEC_PER_SEC, 0.1*NSEC_PER_SEC);
//执行事件
dispatch_source_set_event_handler(_sourceTimer, ^{
self.progressView.progress += 0.1; //销毁定时器
dispatch_source_cancel(_sourceTimer);
}); //启动计时器
dispatch_resume(_sourceTimer);
}*/ - (void)setProgress:(CGFloat)progress{
NSLog(@"进度进行时。。。。。。。。。。。。。。。。。。%f",progress);
_progress = progress;
_progressView.progress = progress;
self.numberLabel.text = [NSString stringWithFormat:@"%.f %%", * progress];
if (progress >= ) {
[self closeBtnClick];
}
} @end
AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中的更多相关文章
- [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)
Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.Lo ...
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...
- [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型
[iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型 作者:u0u0 - iTyran 在上一节中,我们分析了OBJ格式.OBJ格式优点是文本形式,可读 ...
- 教您如何在Word的mathtype加载项中修改章节号
在MathType数学公式编辑器中,公式编号共有五部分内容:分别是章编号(Chapter Number).节编号(Section Number).公式编号(Equation Number).括号(En ...
- vs2010 未能正确加载方案中的一个或多个项目
Visual studio在打开解决方案时,往往会碰到一个这样的错误,提示说:未能正确加载方案中的一个或多个项目: 我们可以通过以下步骤来解决该问题:首先,在相应的sln类型文件上点击右键,选择用记事 ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
- 加载 页面 中js 方法
js 文件中 var mingchen= mingchen|| { init: function (){ } }; 文件中 mingchen.init(); 注意问题: 在新加载 页面中 ...
- WPF中动态加载XAML中的控件
原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...
- 用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机
原文:用MVVM模式开发中遇到的零散问题总结(5)--将动态加载的可视元素保存为图片的控件,Binding刷新的时机 在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打 ...
随机推荐
- 录音-树莓派USB摄像头话筒
实测可用: sudo arecord --duration=10 --device=plughw:1,0 --format=cd aaa.wav sudo arecord --duration=10 ...
- s:iterator
s:iterator 标签有3个属性: value:被迭代的集合 id :指定集合里面的元素的id status 迭代元素的索引 1:jsp页面定义元素写法 数组或list ...
- JAVA变量初始化赋值null
在Java中,null值表示引用不指向任何对象.运行过程中系统发现使用了这样一个引用时·可以立即停止进一步的访问,不会给系统带来任何危险. 1.如果是对象的field的话那么系统在初始化对象的时候会 ...
- Sublime Text 相关教程(转)
曾经有人说过,世界上有两种编辑器,好用和不好用的:而在好用的编辑器中,又分两种,免费的和死贵死贵的.譬如说VIM 和 TextMate,就是免费和死贵的典型.很不幸,今天的主角 Sublime Tex ...
- 程序员代码面试指南:IT名企算法与数据结构题目最优解
第1章栈和队列 1设计一个有getMin功能的栈(士★☆☆☆) 1由两个栈组成的队列(尉★★☆☆) 5如何仅用递归函数和栈操作逆序一个栈(尉★★☆☆) 8猫狗队列(士★☆☆☆)10用一个栈实现另一 ...
- Python的GIL是什么鬼,多线程性能究竟如何
前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几 ...
- [SPOJ1557] Can you answer these queries II
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2482 [算法] 线段树维护历史最值 时间复杂度 : O(NlogN) [代码] #i ...
- 基于区域的全卷积神经网络(R-FCN)简介
在 Faster R-CNN 中,检测器使用了多个全连接层进行预测.如果有 2000 个 ROI,那么成本非常高. feature_maps = process(image)ROIs = region ...
- MakeFile 文件的使用
什么是Makefile? 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译, ...
- Azure SQL Database (27) 创建Table Partition
<Windows Azure Platform 系列文章目录> 昨天客户正好提到这个问题,现在记录一下. 我们在使用传统的SQL Server,会使用Table Partition,这个功 ...