#import "ViewController.h" #import "AFNetworking/AFNetworking.h" @interface ViewController () @end @implementation ViewController -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3782685451,3066622536&fm=27&gp=0.jpg"]]; NSURLSessionDownloadTask *downTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { //打印下载进度 NSLog(@"%f",1.0 * downloadProgress.completedUnitCount/downloadProgress.totalUnitCount); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *fullPath = [filePath stringByAppendingPathComponent:response.suggestedFilename]; NSLog(@"%@",fullPath); return [NSURL fileURLWithPath:fullPath]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { NSLog(@"%@",filePath); NSLog(@"completionHandler----%@",error); }]; [downTask resume]; } @end
本文展示如何利用AFNetworking框架在iOS应用中实现图片下载功能。通过创建AFHTTPSessionManager实例,设置请求并启动下载任务,同时展示了如何监听下载进度、指定缓存路径以及处理下载完成后的回调。
3169

被折叠的 条评论
为什么被折叠?



