2013-05-17 39 views
0

我從網上檢索圖像及其信息(來源和一些其他信息)。我如何將它存儲在iPhone上?我看到使用NSFileManager來存儲圖像,但我怎樣才能存儲圖像和其他一些信息呢?我應該使用NSDictionary和鍵如 - 圖像,起源,信息,然後將該字典存儲到NSMutableArray,然後將該數組存儲到NSFileManager?或者有其他方法嗎?訪問這些保存的圖像和相應的信息的小提示會很好。在iPhone上保存圖像和圖像的信息

謝謝。

+0

嘗試此鏈接http://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app – iDeveloper

+0

我得到了一些代碼,只存儲圖像,但有關「配對」信息與圖像和存儲它,要知道哪些信息屬於哪個圖像,這就是我需要的。不管怎麼說,還是要謝謝你。 – user1832330

+0

你是說[存儲圖像及其exif數據](http://stackoverflow.com/a/5294574/1407017)? – Amar

回答

1
// Download Image from Remote Server 
NSURL *url = [NSURL URLWithString:@"Your IMAGE URL HERE"]; 
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"moon" ofType:@"jpg"]]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
NSString *libPath = [paths objectAtIndex:0]; 
NSString *filePath = [libPath stringByAppendingPathComponent:[url lastPathComponent]]; 
BOOL status = [data writeToFile:filePath atomically:YES]; 
// Save Image file to Library Directory, if success then store infor about it in file 
if(status) 
{ 
    // If File Exist then read it otherwise creat new 
    NSMutableDictionary *imageInfoDict; 
    if([[NSFileManager defaultManager] fileExistsAtPath:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]]) 
    { 
     NSData *fileData = [NSData dataWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]]; 
     imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]]; 

     // To set As Background Load image from disc 
     // Read filename from Dictionary, but you must know the key which you want to get 
     NSString *fileName = imageInfoDict[@"68051_10151509108346729_1731694342_s.png"][@"imagePath"]; 
     // Set your image as background 
     UIImage *image = [UIImage imageWithContentsOfFile:[libPath stringByAppendingPathComponent:fileName]]; 
    } 
    else 
     imageInfoDict = [NSMutableDictionary dictionaryWithCapacity:0]; 

    // Create Dictionary to store Single Image Info 
    NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:[url lastPathComponent], @"imagePath", 
           [url lastPathComponent], @"imageName", 
           [NSDate date], @"date",nil]; 
    // Add Single Image Info to Main Dictionary 
    [imageInfoDict setValue:imageInfo forKey:[url lastPathComponent]]; 

    // Convert Main info Dictionary to `NSData` to Save on Disc 
    NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:imageInfoDict]; 
    // Save file to Disc 
    [fileData writeToFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"] atomically:YES]; 

    // Read Info From File 
    NSLog(@"%@",[NSDictionary dictionaryWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]]); 
} 
+0

非常感謝。這看起來不錯,會測試它,並讓你知道它是如何工作的。謝謝。 – user1832330

+0

還有一件事,請我真的需要。現在,如果我想從路徑訪問該圖像並將其設置爲UIImageView * imgView的背景,我該如何實現這一目標?我怎樣才能訪問相同的圖像「日期」(你設置爲例)鍵?謝謝。 – user1832330

+0

是的日期是示例鍵,設置爲背景我將更新我的代碼,但你應該知道你想要設置爲背景的圖像的關鍵 –

0

許多方面都存在不同upong您的要求

1)SQLITE:保存你的圖像元數據在表格與文件名一起在一排

2)NSUserDefaults的:如果圖像的數量是相當更少

3)文本文件:寫入數據由新行分隔的數據,應該也不難,這取決於您的要求。

希望這會有所幫助。

+0

對於userdefault,20張圖像太多了? – user1832330

+0

應該沒問題。 – Devang

0

你絕對應該使用CoreData和應用程序文件夾是這樣的:

  1. 從服務器下載數據:圖像和其他數據
  2. 保存圖像中的應用程序文件與生成的名稱的文件夾(如果您還沒有服務器的映像名稱)
  3. 根據您的需要創建CoreData表(或多個表),並在其中設置映像名稱和其他屬性。 4當您嘗試加載數據時,您可以從核心數據表中獲取數據並從文檔目錄加載圖像。

我認爲這是最好的和最簡單的方法來做到這一點,如果你只是一次下載圖像。

如果你想下載不同的圖像或更新圖像,你可以使用相同的過程,並提到你不應該將圖像保存到文檔文件夾,你只需要使用一個AsyncImageDownload庫(大量)來下載圖像,您必須將圖像URL存儲在數據庫中。