欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)網(wǎng)絡(luò)篇—實(shí)現(xiàn)大文件的多線程斷點(diǎn)下載

 更新時間:2016年11月03日 10:53:50   作者:文頂頂  
iOS開發(fā)中經(jīng)常會用到文件的下載功能,這篇文章主要介紹了iOS開發(fā)網(wǎng)絡(luò)篇—實(shí)現(xiàn)大文件的多線程斷點(diǎn)下載,今天咱們來分享一下思路。

說明:本文介紹多線程斷點(diǎn)下載。項目中使用了蘋果自帶的類,實(shí)現(xiàn)了同時開啟多條線程下載一個較大的文件。因?yàn)閷?shí)現(xiàn)過程較為復(fù)雜,所以下面貼出完整的代碼。

實(shí)現(xiàn)思路:下載開始,創(chuàng)建一個和要下載的文件大小相同的文件(如果要下載的文件為100M,那么就在沙盒中創(chuàng)建一個100M的文件,然后計算每一段的下載量,開啟多條線程下載各段的數(shù)據(jù),分別寫入對應(yīng)的文件部分)。

項目中用到的主要類如下:

完成的實(shí)現(xiàn)代碼如下:

主控制器中的代碼:

#import "YYViewController.h"
#import "YYFileMultiDownloader.h"

@interface YYViewController ()
@property (nonatomic, strong) YYFileMultiDownloader *fileMultiDownloader;
@end

@implementation YYViewController
- (YYFileMultiDownloader *)fileMultiDownloader
{
  if (!_fileMultiDownloader) {
    _fileMultiDownloader = [[YYFileMultiDownloader alloc] init];
    // 需要下載的文件遠(yuǎn)程URL
    _fileMultiDownloader.url = @"http://192.168.1.200:8080/MJServer/resources/jre.zip";
    // 文件保存到什么地方
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"];
    _fileMultiDownloader.destPath = filepath;
  }
  return _fileMultiDownloader;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  [self.fileMultiDownloader start];
}

@end

自定義一個基類

YYFileDownloader.h文件

#import <Foundation/Foundation.h>

@interface YYFileDownloader : NSObject
{
  BOOL _downloading;
}
/**
 * 所需要下載文件的遠(yuǎn)程URL(連接服務(wù)器的路徑)
 */
@property (nonatomic, copy) NSString *url;
/**
 * 文件的存儲路徑(文件下載到什么地方)
 */
@property (nonatomic, copy) NSString *destPath;

/**
 * 是否正在下載(有沒有在下載, 只有下載器內(nèi)部才知道)
 */
@property (nonatomic, readonly, getter = isDownloading) BOOL downloading;

/**
 * 用來監(jiān)聽下載進(jìn)度
 */
@property (nonatomic, copy) void (^progressHandler)(double progress);

/**
 * 開始(恢復(fù))下載
 */
- (void)start;

/**
 * 暫停下載
 */
- (void)pause;
@end

YYFileDownloader.m文件

#import "YYFileDownloader.h"
 
@implementation YYFileDownloader
@end
下載器類繼承自YYFileDownloader這個類

YYFileSingDownloader.h文件
#import "YYFileDownloader.h"

@interface YYFileSingleDownloader : YYFileDownloader
/**
 * 開始的位置
 */
@property (nonatomic, assign) long long begin;
/**
 * 結(jié)束的位置
 */
@property (nonatomic, assign) long long end; 
@end
YYFileSingDownloader.m文件
#import "YYFileSingleDownloader.h"
@interface YYFileSingleDownloader() <NSURLConnectionDataDelegate>
/**
 * 連接對象
 */
@property (nonatomic, strong) NSURLConnection *conn;

/**
 * 寫數(shù)據(jù)的文件句柄
 */
@property (nonatomic, strong) NSFileHandle *writeHandle;
/**
 * 當(dāng)前已下載數(shù)據(jù)的長度
 */
@property (nonatomic, assign) long long currentLength;
@end

@implementation YYFileSingleDownloader

- (NSFileHandle *)writeHandle
{
  if (!_writeHandle) {
    _writeHandle = [NSFileHandle fileHandleForWritingAtPath:self.destPath];
  }
  return _writeHandle;
}

/**
 * 開始(恢復(fù))下載
 */
- (void)start
{
  NSURL *url = [NSURL URLWithString:self.url];
  // 默認(rèn)就是GET請求
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  // 設(shè)置請求頭信息
  NSString *value = [NSString stringWithFormat:@"bytes=%lld-%lld", self.begin + self.currentLength, self.end];
  [request setValue:value forHTTPHeaderField:@"Range"];
  self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
  
  _downloading = YES;
}

/**
 * 暫停下載
 */
- (void)pause
{
  [self.conn cancel];
  self.conn = nil;
  
  _downloading = NO;
}


#pragma mark - NSURLConnectionDataDelegate 代理方法
/**
 * 1. 當(dāng)接受到服務(wù)器的響應(yīng)(連通了服務(wù)器)就會調(diào)用
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
  
}

/**
 * 2. 當(dāng)接受到服務(wù)器的數(shù)據(jù)就會調(diào)用(可能會被調(diào)用多次, 每次調(diào)用只會傳遞部分?jǐn)?shù)據(jù))
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  // 移動到文件的尾部
  [self.writeHandle seekToFileOffset:self.begin + self.currentLength];
  // 從當(dāng)前移動的位置(文件尾部)開始寫入數(shù)據(jù)
  [self.writeHandle writeData:data];
  
  // 累加長度
  self.currentLength += data.length;
  
  // 打印下載進(jìn)度
  double progress = (double)self.currentLength / (self.end - self.begin);
  if (self.progressHandler) {
    self.progressHandler(progress);
  }
}

/**
 * 3. 當(dāng)服務(wù)器的數(shù)據(jù)接受完畢后就會調(diào)用
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  // 清空屬性值
  self.currentLength = 0;
  
  // 關(guān)閉連接(不再輸入數(shù)據(jù)到文件中)
  [self.writeHandle closeFile];
  self.writeHandle = nil;
}

/**
 * 請求錯誤(失敗)的時候調(diào)用(請求超時\斷網(wǎng)\沒有網(wǎng), 一般指客戶端錯誤)
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  
}

@end

設(shè)計多線程下載器(利用HMFileMultiDownloader能開啟多個線程同時下載一個文件)

一個多線程下載器只下載一個文件

YYFileMultiDownloader.h文件

#import "YYFileDownloader.h"
@interface YYFileMultiDownloader : YYFileDownloader
@end

YYFileMultiDownloader.m文件

#import "YYFileMultiDownloader.h"
#import "YYFileSingleDownloader.h"

#define YYMaxDownloadCount 4

@interface YYFileMultiDownloader()
@property (nonatomic, strong) NSMutableArray *singleDownloaders;
@property (nonatomic, assign) long long totalLength;
@end

@implementation YYFileMultiDownloader

- (void)getFilesize
{
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  request.HTTPMethod = @"HEAD";
  
  NSURLResponse *response = nil;
#warning 這里要用異步請求
  [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
  self.totalLength = response.expectedContentLength;
}

- (NSMutableArray *)singleDownloaders
{
  if (!_singleDownloaders) {
    _singleDownloaders = [NSMutableArray array];
    
    // 獲得文件大小
    [self getFilesize];
    
    // 每條路徑的下載量
    long long size = 0;
    if (self.totalLength % YYMaxDownloadCount == 0) {
      size = self.totalLength / YYMaxDownloadCount;
    } else {
      size = self.totalLength / YYMaxDownloadCount + 1;
    }
    
    // 創(chuàng)建N個下載器
    for (int i = 0; i<YYMaxDownloadCount; i++) {
      YYFileSingleDownloader *singleDownloader = [[YYFileSingleDownloader alloc] init];
      singleDownloader.url = self.url;
      singleDownloader.destPath = self.destPath;
      singleDownloader.begin = i * size;
      singleDownloader.end = singleDownloader.begin + size - 1;
      singleDownloader.progressHandler = ^(double progress){
        NSLog(@"%d --- %f", i, progress);
      };
      [_singleDownloaders addObject:singleDownloader];
    }
    
    // 創(chuàng)建一個跟服務(wù)器文件等大小的臨時文件
    [[NSFileManager defaultManager] createFileAtPath:self.destPath contents:nil attributes:nil];
    
    // 讓self.destPath文件的長度是self.totalLengt
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.destPath];
    [handle truncateFileAtOffset:self.totalLength];
  }
  return _singleDownloaders;
}

/**
 * 開始(恢復(fù))下載
 */
- (void)start
{
  [self.singleDownloaders makeObjectsPerformSelector:@selector(start)];
  
  _downloading = YES;
}

/**
 * 暫停下載
 */
- (void)pause
{
  [self.singleDownloaders makeObjectsPerformSelector:@selector(pause)];
  _downloading = NO;
}

@end

補(bǔ)充說明:如何獲得將要下載的文件的大小?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中日志同步獲取NSLog重定向以及其他詳解

    iOS中日志同步獲取NSLog重定向以及其他詳解

    在Objective-c開發(fā)程序的時候,有專門的日志操作類NSLog,它將指定的輸出,輸出到(stderr),我們可以利用Xcode的日志輸出窗口,下面這篇文章主要給大家介紹了關(guān)于iOS中日志同步獲取NSLog重定向以及其他的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • xcode8提交ipa失敗無法構(gòu)建版本問題的解決方案

    xcode8提交ipa失敗無法構(gòu)建版本問題的解決方案

    xcode升級到xcode8后發(fā)現(xiàn)構(gòu)建不了新的版本。怎么解決呢?下面小編給大家?guī)砹藊code8提交ipa失敗無法構(gòu)建版本問題的解決方案,非常不錯,一起看看吧
    2016-10-10
  • iOS實(shí)現(xiàn)波浪效果

    iOS實(shí)現(xiàn)波浪效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)波浪效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • IOS視圖控制器的生命周期實(shí)例詳解

    IOS視圖控制器的生命周期實(shí)例詳解

    這篇文章主要介紹了IOS視圖控制器的生命周期實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • iOS SwiftUI 顏色漸變填充效果的實(shí)現(xiàn)

    iOS SwiftUI 顏色漸變填充效果的實(shí)現(xiàn)

    這篇文章主要介紹了iOS SwiftUI 顏色漸變填充效果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • iOS如何自定義啟動界面實(shí)例詳解

    iOS如何自定義啟動界面實(shí)例詳解

    當(dāng)我們打開一款應(yīng)用程序的時候,首先映入眼簾的往往并不是程序的主界面,而是經(jīng)過精心設(shè)計的歡迎界面,這個界面通常會停留幾秒鐘,然后消失。下面這篇文章主要給大家介紹了關(guān)于iOS如何自定義啟動界面的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • iOS移動端軟鍵盤彈起空白和滾動穿透問題解決方案

    iOS移動端軟鍵盤彈起空白和滾動穿透問題解決方案

    這篇文章主要為大家介紹了iOS移動端軟鍵盤彈起空白和滾動穿透問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • iOS開發(fā)中關(guān)鍵字const/static/extern、UIKIT_EXTERN的區(qū)別和用法

    iOS開發(fā)中關(guān)鍵字const/static/extern、UIKIT_EXTERN的區(qū)別和用法

    這篇文章主要介紹了iOS 關(guān)鍵字const/static/extern、UIKIT_EXTERN區(qū)別和用法,需要的朋友可以參考下
    2017-12-12
  • 簡單說說iOS之WKWebView的用法小結(jié)

    簡單說說iOS之WKWebView的用法小結(jié)

    iOS8.0之后我們使用 WebKit框架中的WKWebView來加載網(wǎng)頁。這篇文章主要介紹了簡單說說iOS之WKWebView的用法小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • iOS組件依賴避免沖突的小技巧分享

    iOS組件依賴避免沖突的小技巧分享

    這篇文章主要給大家介紹了關(guān)于iOS組件依賴避免沖突的小技巧,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論