美文网首页
iOS AFNetworking 下载图片视频

iOS AFNetworking 下载图片视频

作者: 冬日的太阳_c107 | 来源:发表于2019-03-06 10:40 被阅读0次

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

        [self download];

    }

    //下载

    -(void)download

    {

        //1.创建会话管理者

        AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

        NSURL *url = [NSURL URLWithString:@"https://v.qq.com/x/page/b0837pjcueo.html"];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        //2.下载文件

        /*

         第一个参数:请求对象

         第二个参数:progress 进度回调 downloadProgress

         第三个参数:destination 回调(目标位置)

         有返回值

         targetPath:临时文件路径

         response:响应头信息

         第四个参数:completionHandler 下载完成之后的回调

         filePath:最终的文件路径

         */

       /*

         第一个参数:请求对象

         第二个参数:进度回调

         downloadProgress.completedUnitCount :已经下载的数据

         downloadProgress.totalUnitCount:数据的总大小

         第三个参数:destination回调,该block需要返回值(NSURL类型),告诉系统应该把文件剪切到什么地方

         targetPath:文件的临时保存路径tmp,随时可能被删除

         response:响应头信息

         第四个参数:completionHandler请求完成后回调

         response:响应头信息

         filePath:文件的保存路径,即destination回调的返回值

         error:错误信息

         */

        NSURLSessionDownloadTask*download = [managerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress) {

            //监听下载进度

            //completedUnitCount 已经下载的数据大小

            //totalUnitCount    文件数据的中大小

            NSLog(@"%f",1.0*downloadProgress.completedUnitCount/ downloadProgress.totalUnitCount);

        }destination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {

            /**

             * 1:1:请求路径:NSUrl *url = [NSUrl urlWithString:path];从网络请求路径  2:把本地的file文件路径转成url,NSUrl *url = [NSURL fileURLWithPath:fullPath];

             2:返回值是一个下载文件的路径

             *

             */

            NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];

            NSLog(@"targetPath:%@",targetPath);

            NSLog(@"fullPath:%@",fullPath);

            return[NSURLfileURLWithPath:fullPath];

        }completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {

            /**

             *filePath:下载后文件的保存路径

             */

            NSData *data = [NSData dataWithContentsOfURL:filePath];

        /// UIImage *image = [UIImage imageWithData:data]; // 取得图片

            NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;

            NSString *imageFilePath = [path stringByAppendingPathComponent:@"MyImage"];

            [datawriteToFile:imageFilePathatomically:YES];

    //        BOOL success = [UIImageJPEGRepresentation(image, 0.5) writeToFile:imageFilePath  atomically:YES];

    //        if (success){

    //            NSLog(@"写入本地成功");

    //        }

    //        NSLog(@"%@",filePath);

        }];

        //3.执行Task

        [downloadresume];

    }

    相关文章

      网友评论

          本文标题:iOS AFNetworking 下载图片视频

          本文链接:https://www.haomeiwen.com/subject/vyttpqtx.html