2016-07-12 52 views
0

的頭銜,我怎麼能存儲圖像和plist任意字符串值作爲dictionary.I 已經創建了一個plist文件中的項目,也 我怎麼可以附加與不同的相同dictionary文件圖像和名稱?保存圖像及其對的plist

+0

it dup licate http://stackoverflow.com/questions/2486705/storing-image-in-plist –

+0

你如何得到圖像? – user3182143

+0

你從照片庫中選擇圖像還是直接從圖像包中獲取圖像? – user3182143

回答

0

您可以點擊此鏈接。它提供了詳細介紹如何存儲圖像的plist

Storing image in plist

併爲標題來存儲您可以添加新的鍵像下面。

//獲取一個完整路徑的文檔文件夾內的plist中的應用

NSString *titleStr = @"Title Of Image"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, 
                YES); 
NSString *path = [NSString stringWithFormat:@"%@/YOURPLIST.plist", 
               [paths objectAtIndex:0]]; 

// Place an image in a dictionary that will be stored as a plist 
[dictionary setObject:image forKey:@"image"]; 
[dictionary setObject:titleStr forKey:@"title"]; 

// Write the dictionary to the filesystem as a plist 
[NSKeyedArchiver archiveRootObject:dictionary toFile:path]; 
+0

它適合您嗎? – Bhoomi

0

使用下面的代碼編寫圖像&標題,聚集在plist-

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"PlistName.plist"]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath: path]) 
    { 
     path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"PlistName.plist"] ]; 
    } 

    fileManager = [NSFileManager defaultManager]; 
    NSMutableDictionary *dataDictionary=[NSMutableDictionary alloc] init]; 


    [dataDictionary setObject:ImageData forKey:image]; 
    [dataDictionary setObject:ImageTitle forKey:title]; 
    [dataDictionary writeToFile: path atomically:YES]; 

而且看你的圖片&標題字典從plist這樣 -

- (NSDictionary *)readDataFromPlist() 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"PlistName.plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSMutableDictionary *dataDictionary; 

    if ([fileManager fileExistsAtPath: path]) 
    { 
     dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
     return dataDictionary; 
    }else 
     return nil; 

}