在plist上保存图像及其标题

时间:2016-07-12 10:49:47

标签: ios objective-c plist

如何将图像和任何字符串值存储在Invalid parameter "alanong-pc\alan"中作为字典.I 已在项目中创建了plist文件,并且 另外我如何使用相同plist的不同图像和名称附加文件?

2 个答案:

答案 0 :(得分:0)

您可以点按此链接。它提供了如何将图像存储到plist的详细信息

Storing image in plist

对于存储的标题,您可以添加如下所示的新密钥。

//获取应用程序的Documents文件夹中的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];

答案 1 :(得分: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;

}