實現(xiàn)協(xié)議NSURLSessionDownloadDelegate的2個方法
此方法主要返回文件的字節(jié)數(shù)以及已下載的字節(jié)數(shù)
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
儲存的零時文件路徑location
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location;
樣式圖锨匆,由于網(wǎng)速太快亿扁,手速跟不上如蚜,加載圖片太快沒來得及點擊暫停下載以及就加載出來了择镇,大家如果想實現(xiàn)可以把路徑改為一個大一點的文件挡逼,這樣就能看的很明顯了。
代碼實現(xiàn)
#import "ViewController.h"
@interface ViewController () <NSURLSessionDownloadDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *loadImage;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (weak, nonatomic) IBOutlet UILabel *loadPlan;
@property (weak, nonatomic) IBOutlet UIButton *loadTitle;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *task;
@property (nonatomic, assign) BOOL judgeDown;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 下載的字節(jié)
*
* @param session session
* @param downloadTask taskdownload
* @param bytesWritten 每次下載的字節(jié)
* @param totalBytesWritten 已下載的字節(jié)
* @param totalBytesExpectedToWrite 總字節(jié)
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
//進度條計算
CGFloat progress = (totalBytesWritten*1.0)/totalBytesExpectedToWrite;
//異步添加到主隊列下載
dispatch_async(dispatch_get_main_queue(), ^{
_progress.progress = progress;
_loadPlan.text = [NSString stringWithFormat:@"開始下載:%d/100",(int)(progress*100)];
}) ;
}
/**
* NSURLSessionDownloadDelegate委托協(xié)議
*
* @param session session
* @param downloadTask sessiontask
* @param location 儲存的零時文件路徑
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSFileManager *manager = [NSFileManager defaultManager];
//獲取document目錄
NSString *strDocument = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
//在原有路徑上添加路徑
NSString *strURL = [strDocument stringByAppendingPathComponent:@"1250538257738.jpg"];
//本地URL路徑
NSURL *url = [NSURL fileURLWithPath:strURL];
if ([manager fileExistsAtPath:strURL]) {
[manager removeItemAtPath:strURL error:nil];
}
//把tem文件移動指定的URL路徑上
if ([manager moveItemAtURL:location toURL:url error:nil]) {
_data = [manager contentsAtPath:strURL];
_loadImage.image = [UIImage imageWithData:_data];
}
}
- (IBAction)onClick:(id)sender {
NSString *strURL = @"http://img.pconline.com.cn/images/bbs4/20098/18/1250538257738.jpg";
NSURL *url = [NSURL URLWithString:strURL];
//創(chuàng)建config對象
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//創(chuàng)建session對象腻豌,并添加到主隊列中
_session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//下載路徑
_task = [_session downloadTaskWithURL:url];
[_task resume];
}
- (IBAction)suspendLoad:(id)sender {
if (_judgeDown) {
NSLog(@"恢復下載");
_loadTitle.titleLabel.text = @"暫停下載";
if(!_data){
NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/bbs4/20098/18/1250538257738.jpg"];
// _request = [NSURLRequest requestWithURL:url];
// _task = [_session downloadTaskWithRequest:_request];
_task = [_session downloadTaskWithURL:url];
}else{
_task = [_session downloadTaskWithResumeData:_data];
}
[_task resume];
}else{
NSLog(@"暫停下載");
_loadTitle.titleLabel.text = @"恢復下載";
//取消下載并調(diào)用回調(diào)與恢復數(shù)據(jù)供以后使用
[_task cancelByProducingResumeData:^(NSData *resumeData) {
_data = resumeData;
}];
_task = nil;
_judgeDown = YES;
}
}
@end