图片下载添加进度条
2016-03-01 23:25
225 查看
//
// ProgressView.h
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ProgressView :
UIView
@property (nonatomic,
assign) CGFloat progress;
@end
//
// ProgressView.m
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ProgressView.h"
@implementation ProgressView
- (void)setProgress:(CGFloat)progress {
_progress = progress;
[self
setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *path = [UIBezierPath
bezierPathWithRect:CGRectMake(0,
0, self.frame.size.width
* self.progress,
self.frame.size.height)];
[[UIColor
whiteColor] set];
[path fill];
}
@end
//
// ViewController.m
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "ProgressView.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UIImageView *imageView;
@property (weak,
nonatomic) IBOutlet
ProgressView *progressView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
__weak typeof(self) weakSelf =
self;
[self.imageView
sd_setImageWithURL:[NSURL
URLWithString:@"http://imgs.it007.com/data/attachment/forum/201511/26/174705khwuujwn82k2peh7.jpg.thumb.jpg"]
placeholderImage:nil
options:0
progress:^(NSInteger receivedSize,
NSInteger expectedSize) {
weakSelf.progressView.hidden =
NO;
weakSelf.progressView.progress = (CGFloat)receivedSize / expectedSize;
} completed:^(UIImage *image,
NSError *error, SDImageCacheType cacheType,
NSURL *imageURL) {
weakSelf.imageView.image = image;
weakSelf.progressView.hidden =
YES;
}];
}
@end
// ProgressView.h
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ProgressView :
UIView
@property (nonatomic,
assign) CGFloat progress;
@end
//
// ProgressView.m
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ProgressView.h"
@implementation ProgressView
- (void)setProgress:(CGFloat)progress {
_progress = progress;
[self
setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *path = [UIBezierPath
bezierPathWithRect:CGRectMake(0,
0, self.frame.size.width
* self.progress,
self.frame.size.height)];
[[UIColor
whiteColor] set];
[path fill];
}
@end
//
// ViewController.m
// downloadImage
//
// Created by zmx on 16/3/1.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "ProgressView.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UIImageView *imageView;
@property (weak,
nonatomic) IBOutlet
ProgressView *progressView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
__weak typeof(self) weakSelf =
self;
[self.imageView
sd_setImageWithURL:[NSURL
URLWithString:@"http://imgs.it007.com/data/attachment/forum/201511/26/174705khwuujwn82k2peh7.jpg.thumb.jpg"]
placeholderImage:nil
options:0
progress:^(NSInteger receivedSize,
NSInteger expectedSize) {
weakSelf.progressView.hidden =
NO;
weakSelf.progressView.progress = (CGFloat)receivedSize / expectedSize;
} completed:^(UIImage *image,
NSError *error, SDImageCacheType cacheType,
NSURL *imageURL) {
weakSelf.imageView.image = image;
weakSelf.progressView.hidden =
YES;
}];
}
@end
相关文章推荐
- Android里TextView中显示不下的文字转而显示省略号
- 常见围棋软件资源整理
- 常见围棋软件资源整理
- 设计模式——观察者模式
- ProgressDialog进度对话框使用方法
- C++ namespace功能总结
- TOEFL 学习笔记(writing 8)
- 玩转AI(Adobe illustrator)——小西瓜(4)
- 新技术文章收集
- 一名小白理清关于Web的一些基本概念
- POJ 1082 Calendar Game(找规律博弈)
- 任一个英文的纯文本文件0004.txt,统计其中的单词(不区分大小写)出现的个数
- Java排序一:冒泡排序
- Consumer closed input channel or an error occurred/Channel is unrecoverably broken and will be dispo
- LeetCode 74 Search a 2D Matrix(搜索2D矩阵)
- C 语言构造hash table 解 LC majority element问题
- EL操作 web 对象的常用方法
- 蓝牙获取MAC地址并转换成字符串形式
- 关于<uses-library android:name="android.test.runner"/>
- 作品第四课----agruments应用一求出函数参数的总合