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(@"写入失败");
            }
        }];

     

  • 相关阅读:
    使用yum 安装时出现 : Loaded plugins: fastestmirror
    三级店铺发布店铺悬赏必看
    专属包发布说明
    发布店铺内容(原封不动的直接复制内容发布就可以)
    tomcat 专属图片文件夹配置
    java websocket时时通信
    lua面向对象
    jenkins 查看请求调用栈信息
    从一个简单的例子看spring ApplicationContext上下文隔离
    java异常查看利器之使用 jvmti 的Callback_JVMTI_EVENT_EXCEPTION 事件查看异常
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/4963343.html
Copyright © 2011-2022 走看看