如何在plist中存储图像path?

我知道这可能是一个愚蠢的问题,但我将大部分游戏数据存储在plist中 – 我希望在游戏中包含对图像的引用 – 与“支持文件”相同的层次级别。 我有不同types的图像存储在3个单独的文件夹。 一个文件夹例如叫imageclue。 我怎么能在我的plist存储path,我卡住,因为我不能只存储在我的plistpath为string – filename.jpg。 我已经尝试获取文件的path,但是当我注销它。

对不起,如果我不解释,并提前谢谢你的帮助:)

编辑**

我有一个plist文件添加到我的程序我不想以编程方式添加到它,因为图像是常量 – 下面的屏幕截图显示一个教程,而不是filename.jpg(因为这将不会看到我的图像存储在一个文件)我想知道什么path名称作为一个string使用。

在这里输入图像说明

该图片来自appcoda.com的教程 – 它说缩略图是图片path文件。 如果您查看图像存储在左侧的位置 – 它们将与程序文件一起存储。 我的图像在那里的一个文件夹,所以我很困惑,什么进入我的plist图像文件。

希望这清理了我的意思,对不起:)

在.h文件中存储三个variables

@interface YourViewController : UIViewController { NSString *folder1; NSString *folder2; NSString *folder3; } 

在viewdidload中:

 -(void) viewdidLoad { //get the documents directory: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //getting the folder name: folder1 = [NSString stringWithFormat:@"%@/imageclue", documentsDirectory]; folder2 = [NSString stringWithFormat:@"%@/folder2", documentsDirectory]; folder3 = [NSString stringWithFormat:@"%@/folder3", documentsDirectory]; } -(NSArray*) getPlistFromFolder:(NSString*)folder imageName:(NSString*)image { NSString *imageTitle = [NSString stringWithFormat:@"%@/image", folder]; NSArray *data = [[NSArray alloc] initWithContentsOfFile:plistName]; return data; } 

所以在plist文件中,只需存储图像名称。

希望这可以帮助…

像这样做,

 NSDictionary *imagePaths = @{@"image 1": [NSHomeDirectory() stringByAppendingPathComponent:@"image 1"]}; [self writeToPlist:imagePaths]; - (void)writeToPlist:imagePaths:(id)plist{ NSError *error; NSData *data = [NSPropertyListSerialization dataWithPropertyList:plist format:kCFPropertyListXMLFormat_v1_0 options:0 error:&error]; if(error){ NSLog(@"Could not write to file"); return; } [data writeToFile:[self plistPath] atomically:YES]; } 

像明智的加载一样简单,

 [self loadImagePathForImageNamed:@"image 1"]; - (NSString*)loadImagePathForImageNamed:(NSString*)imageName{ } - (NSString*)loadImagePathForImageNamed:(NSString*)imageName{ NSData *data = [NSData dataWithContentsOfFile:[self plistPath]]; NSString *error; NSPropertyListFormat format; NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; if(error){ NSLog(@"Could not open plist %@", error); return nil; } return dictionary[imageName]; } 

当文件不存在时,可能需要通过创build新文件来处理错误,否则应该起作用。

您正在存储path正确的方式,只需要将图像的文件名与扩展名存储在plist中,当您的图像在您的应用程序包中时,为了更多的参考,您可以定义键名而不是plist中的“item1”,“item2”。

现在来到实际的问题,如何从plist访问图像

第1步:从Application Bundle中读取您的recipes.plist

 NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePath]; 

第2步:现在获取图像/缩略图名称,你想要加载

步骤3:在控制器中定义以下函数,该函数返回名称的图像

 - (UIImage *)getImageWithName:(NSString *)imageFileName { NSString *ext = [imageFileName pathExtension]; NSString *imagePath = [[NSBundle mainBundle] pathForResource:[imageFileName stringByDeletingPathExtension] ofType:ext]; return [UIImage imageWithContentsOfFile:imagePath]; } 

如何使用

假设你想用“Item2”键加载Image然后写下面的代码

 NSString *imageFileName = [[dict objectForKey:@"Thumbnail"] valueForKey:@"Item2"]; UIImage *item2Image = [self getImageWithName:imageFileName]; 

对于“Item6”

 NSString *imageFileName1 = [[dict objectForKey:@"Thumbnail"] valueForKey:@"Item6"]; UIImage *item6Image = [self getImageWithName:imageFileName1];