使用url动态创建文件夹

时间:2014-02-12 08:47:13

标签: ios iphone objective-c json ipad

我正在创建一个应用程序,我必须从服务器下载一些图像并存储它以在网络不可用时使用(并保存数据)。 我面临的问题是我有一个基本网址(www.url.com/)并且所有图像地址都来自JSON连接,但它是这样的:Upload/Ctop/GC60V/JPEG/GC60V_concept.jpg。由于我有一些不同的图像,每个图像都有不同的路径,我需要以两种方式之一存储它:

  1. 只是Documents/Upload/
  2. 中的文件名
  3. 使用Documents目录中的完整路径创建文件夹。
  4. 对于这两种方式,我必须能够(当然)恢复图像。

    我现在正在做的是: - 我检查服务器中是否有更新。如果存在,我会尝试删除/Upload/文件夹并重新创建它:

    // Erase previous images
            NSFileManager *filemgr = [NSFileManager defaultManager];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [NSString stringWithFormat:@"%@%@", [paths objectAtIndex:0], @"/Upload/"];
    
            NSLog(@"%@", [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil]);
            if ([filemgr removeItemAtPath: documentsDirectory error: NULL]  == YES) {
                NSLog(@"Folder removed: %@", documentsDirectory);
            }
    

    然后,我收到JSON数据,我尝试'创建文件夹'并保存新文件:

    // Download of categories images
                    if (![[NSString stringWithFormat:@"%@%@", URL_BASE, [[categories objectAtIndex:j] valueForKey:@"Path"]] isEqualToString:URL_BASE]) {
                        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_BASE, [[categories objectAtIndex:j] valueForKey:@"Path"]]];
                        urlData = [NSData dataWithContentsOfURL:url];
                    }
    
                    if (urlData)
                    {
                        NSLog(@"Starting categories image download...");
                        NSLog(@"URL: %@", url);
    
                        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, [[categories objectAtIndex:j] valueForKey:@"Path"]];
                        [urlData writeToFile:filePath atomically:YES];
                        NSLog(@"Download finished.");
                    } else {
                        NSLog(@"No categories image to download.");
                    }
    

    据我了解,我错过了正确创建文件夹的过程,但我无法弄清楚我该怎么做。另外,我不知道怎么读它。

    非常感谢任何帮助!

    感谢。

1 个答案:

答案 0 :(得分:1)

首先,当您将图像文件保存到磁盘时,每个图像应具有不同的名称,以便您可以在应用程序中访问它,例如,

假设您从服务器获取图像,您可以像下面一样保存它

     NSURL *url = [NSURL URLWithString:path];
     NSString *picname = [url lastPathComponent];//hear u are getting the image name as the last part of your url
    NSString *imagePath = [NSString stringWithFormat:@"/uplpad/%@",picname]; //hear picname is unique for each download, thus u hav different name for each file u saved

 if(imageData != nil)
    {
        NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
       NSString *pngPath = [NSString stringWithFormat:@"%@%@",Dir,imagePath]; //path means ur destination contain's  this format -> "/foldername/picname" pickname must be unique
       if(![[NSFileManager defaultManager] fileExistsAtPath:[pngPath stringByDeletingLastPathComponent]])
       {
           NSError *error;
          [[NSFileManager defaultManager] createDirectoryAtPath:[pngPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error];
          if(error)
          {
              NSLog(@"error in creating dir");
          }
      }
        [imageData writeToFile:pngPath atomically:YES];
   }


上面的

是将数据保存到上传文件夹中的磁盘

用于检索图像,您需要使用与文件相同的图像名称 例如

    NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString *Path = [NSString stringWithFormat:@"%@%@",Dir,InDestination];//hear InDestination contains the path something like "/upload/uniqueImagename"
   UIImage *image = [UIImage imageWithContentsOfFile:Path];
   if(image)
   { 
     ..do somthing with image
   }
   else
   { 
     ..image not found need to download and save 
   } 


删除文件夹

  NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES) objectAtIndex:0];
 NSString *pathToFolder = [NSString stringWithFormat:@"%@/%@",Dir,@"fbfriends"];

 BOOL remove = [[NSFileManager defaultManager] removeItemAtPath:pathToFolder error:nil];
 if(remove)
 {
     NSLog(@"success");
 }
 else
 {
     NSLog(@"error");
 }