您的位置:首页 > 其它

图片下载添加进度条

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: