AFNetworking 3.0 视频下载 NSOperationQueue 多视频下载

创建一对 VideoOpration 继承于NSOperation  比较简单 作为参考

.h 文件

@interface VideoOpration : NSOperation

@property (nonatomic, strong) NSString *videoUrl; 视频地址

@property (nonatomic, strong) NSString *videoPath; 本地存储地址

@property (nonatomic, strong) NSString *videoPlist; 存放下载列表地址

@property (nonatomic, strong) NSString *videoName; 视频的名字

@property (nonatomic, assign) float progress; 下载进度

- (void)pauseAction; 下载暂停

- (void)continiueAction; 下载继续

@end


.m 文件

@interface VideoOpration()


@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;


@end


@implementation VideoOpration


- (void)main{

    //网络视频地址

    NSURL *URL = [NSURL URLWithString:self.videoUrl];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    //下载Task操作

    self.downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull          downloadProgress) {

        //downloadProgress.totalUnitCount; 文件的总大小

        //downloadProgress.completedUnitCount; 当前已经下载的大小

        // 回到主队列刷新UI

        dispatch_async(dispatch_get_main_queue(), ^{

           self.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;

        });

    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        //- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径

        return [NSURL fileURLWithPath:self.videoPath];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable       error) {

              // 每次APP启动沙盒路径都会改变,,只存储视频名字,,每次取出时获取沙盒路径进行拼接得到视频路径

        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.videoPlist];

        if (arr.count == 0) {

            arr = [NSMutableArray arrayWithObject:self.videoName];

        }else{

            [arr addObject:self.videoName];

        }

        [NSKeyedArchiver archiveRootObject:arr toFile:self.videoPlist];

    }];

     // 开始下载 需要有这句话 没有的话不开始下载

    [self.downloadTask resume];

}

- (void)pauseAction{

    [self.downloadTask suspend]; // 暂停下载

}

- (void)continiueAction{

    [self.downloadTask resume]; // 继续下载

}

创建一对 视频下载工具 DownLoadTool

.h 文件

@interface DownLoadTool : NSObject

 /// 下载视频的接口

- (void)downLoadWithUrl:(NSString *)videoUrl videoPath:(NSString *)videoPath videoPlist:(NSString *)videoPlist videoName:(NSString *)videoName;

@end


.m 文件

- (void)downLoadWithUrl:(NSString *)videoUrl videoPath:(NSString *)videoPath videoPlist:(NSString *)videoPlist videoName:(NSString *)videoName{

    VideoOpration *opration = [[VideoOpration alloc] init]; /// 创建下载操作线程

    // 传递关键信息

    opration.videoUrl = videoUrl;

    opration.videoName = videoName;

    opration.videoPath = videoPath;

    opration.videoPlist = videoPlist;

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [delegate.downLoadQueue addOperation:opration]; /// 将下载线程加进队列里

    [delegate.uploadArray addObject:opration];  /// 将队列加进全局数组

}



在下载列表页 创建tableView 创建一个获取下载进度的计时器

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

- (void)timerAction{

    [self.listTableView reloadData];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *reuse = @"reuse";

    DownLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

    if (!cell) {

        cell = [[DownLoadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];

    }

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    VideoOpration *opration = delegate.uploadArray[indexPath.row];

    [cell refreshui:opration];

    return cell;

}



cell 中 

- (void)refreshui:(VideoOpration *)opration{

    self.videoOption = opration;

    self.progressLabel.text = [NSString stringWithFormat:@"%f",opration.progress];

   // NSLog(@"-=-=-=-=%f",opration.progress);

}


- (void)pauseAction{

    if (self.play == YES) {

        [self.videoOption pauseAction];  暂停点击实现

    }else{

        [self.videoOption continiueAction]; 继续点击实现

    }

    self.play = !self.play;

}





你可能感兴趣的:(AFNetworking 3.0 视频下载 NSOperationQueue 多视频下载)