zoukankan      html  css  js  c++  java
  • NSFileManager(文件管理类)

    NSFileManager(文件管理类)

     文件管理类, 对文件进行管理 

        NSFileManager *fm = [NSFileManager defaultManager];
        NSString *filePath = [NSString stringWithFormat:@"%@/image", NSHomeDirectory()];
        NSLog(@"%@", filePath);
        //创建文件夹
        NSError *error = nil;
        BOOL result = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
        if (result) {
            NSLog(@"创建成功");
        } else {
            NSLog(@"创建失败: %@", error);
        } 

    移动文件夹

        NSString *documentpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
        NSString *filePath1 = [NSString stringWithFormat:@"%@/image", documentpath];
        BOOL result =[fm moveItemAtPath:filePath toPath:filePath1 error:&error];
        if (result) {
            NSLog(@"创建成功");
        } else {
            NSLog(@"创建失败: %@", error);
        }
        NSString *imagePath = [[NSBundle mainBundle] bundlePath];
        NSString *imagePath1 = [filePath1 stringByAppendingPathComponent:@"1.jpg"];
        BOOL result =[fm copyItemAtPath:imagePath toPath:imagePath1 error:&error];
            if (result) {
                NSLog(@"创建成功");
            } else {
                NSLog(@"创建失败: %@", error);
            }

    保存图片

        NSURL *url = [NSURL URLWithString:@"http://img3.douban.com/view/event_poster/median/public/4cdefe4f77918b1.jpg"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSString *filePath = [NSString stringWithFormat:@"%@/1.jpg", NSHomeDirectory()];
            NSLog(@"%@", filePath);
            BOOL result = [data writeToFile:filePath atomically:YES];
            if (result) {
                NSLog(@"写入成功");
            } else {
                NSLog(@"写入失败");
            }
        }];

     

  • 相关阅读:
    String.Intern原来可以减少占用内存···
    一些话···
    javascript 闭包和原型 (转载)
    20100610
    jQuery选择头像
    20100611
    新的一天又开始了····
    13种常用按钮、文本框、表单等CSS样式
    心悸···
    我对她说,你能不能换件衣服?换种心情?换种脾气?她说,可以,换个人就行了···
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/4963343.html
Copyright © 2011-2022 走看看