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

本文介绍了使用AFNetworking 3.0实现视频下载的详细步骤,包括创建自定义NSOperation子类VideoOpration,管理下载进度,并通过NSOperationQueue进行多任务下载。此外,还展示了如何在UITableView中展示下载进度和控制暂停/继续下载功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一对 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;

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值