码迷,mamicode.com
首页 > 其他好文 > 详细

OC网络下载

时间:2016-04-08 11:50:27      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

- (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];

}

 

OC网络下载

标签:

原文地址:http://www.cnblogs.com/yangqinglong/p/5367154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!