2014-02-12 86 views
0

我正在創建一個應用程序,我必須從服務器下載一些圖像並將其存儲以在網絡不可用(並保存數據)時使用。 我面臨的問題是,我有一個基址(www.url.com/),所有的圖像地址來自JSON連接,但它是這樣的:Upload/Ctop/GC60V/JPEG/GC60V_concept.jpg。因爲我有一些不同的圖像,每幅圖像想出了一個不同的路徑,我需要將其存儲在的兩種方法之一:使用url動態創建文件夾

  1. Documents/Upload/
  2. 只是文件名以完整路徑創建文件夾在Documents目錄中。

對於這兩種方式,我必須能夠(當然)恢復圖像。

我現在正在做的是: - 我檢查服務器是否有更新。如果它存在,我試圖抹掉的/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

之前創建目錄'writeToFile:'''[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&err或者];' – Akhilrajtr

+0

我不相信這很簡單@Akhilrajtr(實際上我是這麼做的!)。它現在正在創建文件夾!但我仍然必須解決與文件名的問題,因爲我試圖用完整的路徑保存文件的名稱(類似於/Upload/Folder/etc/image.jpg),我相信我必須剪切文件夾名字,只是讓圖像的名稱。有沒有簡單的方法來做到這一點?感謝您的幫助。 – Tchelow

+0

所以你只需要'Documents/Upload/imageName.jpg'作爲路徑? – Akhilrajtr

回答

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"); 
} 
+0

謝謝你的完整解釋。完美工作! :) – Tchelow

+0

歡迎..... :) –