zoukankan      html  css  js  c++  java
  • OC网络下载

    - (void)viewDidLoad {

        [super viewDidLoad];

        NSURL *url = [NSURL URLWithString:@"http://img4q.duitang.com/uploads/item/201501/13/20150113172039_Ea5ry.jpeg"];

        //可以自己指定临时文件的路径

    //    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"tmppic"];

    //    NSURLCache *urlCache = [[NSURLCache alloc]initWithMemoryCapacity:4*1024*1024 diskCapacity:20*1024*1024 diskPath:filePath];

        NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

    //    defaultConfig.URLCache = urlCache;

        NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfig delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

        NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            //location 临时文件路径

            NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"image"];

            NSLog(@"%@",filePath);

            //将FilePath转化为NSURL类型

            NSURL *fileUrl = [NSURL fileURLWithPath:filePath];

            //将location 文件的内容拷贝到fileurl

            [[NSFileManager defaultManager] copyItemAtURL:location toURL:fileUrl error:nil];

        }];

        [downloadTask resume];

    }

  • 相关阅读:
    [BJWC2010]外星联络
    [NOI2015]品酒大会
    工艺 /【模板】最小表示法
    [NOI2016]优秀的拆分
    [HEOI2016/TJOI2016]字符串
    [SDOI2016]生成魔咒
    【模板】后缀自动机 (SAM)【SA解法】
    [湖南集训]图森
    [USACO17DEC]Standing Out from the Herd P
    Annihilate
  • 原文地址:https://www.cnblogs.com/yangqinglong/p/5367154.html
Copyright © 2011-2022 走看看