【问题标题】:how to store an image path in a plist?如何将图像路径存储在plist中?
【发布时间】:2013-04-25 18:58:09
【问题描述】:

我知道这可能是一个愚蠢的问题,但我将我的大部分游戏数据存储在一个 plist 中 - 我想包含对我游戏中使用的图像的引用 - 与“支持文件”相同的层次结构级别。我有不同类型的图像存储在 3 个单独的文件夹中。例如,一个文件夹称为 imageclue。我怎么能将路径存储在我的 plist 中,我被卡住了,因为我不能将路径存储在我的 plist 中作为字符串 - 文件名.jpg。我已经尝试获取文件的路径,但是当我将其注销时。

对不起,如果我解释得不好,提前感谢您的帮助:)

编辑**

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

图片来自 appcoda.com 的教程 - 它说缩略图是图片路径文件。如果您查看左侧图像的存储位置 - 它们与程序文件一起存储。我的图像在那里的一个文件夹中,所以我对在我的 plist 中输入图像文件的内容感到困惑。

希望这能澄清我的意思,对不起:)

【问题讨论】:

  • 您似乎没有完成问题。你想如何使用图像?您需要每个文件夹中的图像列表吗?
  • 嗨@Maria 我已经发布了使用您的 plist 数据读取图像的答案。祝你好运,让我知道有关它的见解。

标签: ios objective-c


【解决方案1】:

在 .h 文件中存储三个变量

@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文件中,只存储图片名称。

希望这会有所帮助...

【讨论】:

  • 看不懂 NSArray 方法?但感谢您对标头变量的帮助:)。
  • 对不起..我的错误...该方法从plist文件中获取数据...可以根据您的情况进行更改..
  • 如果我从其他地方的 plist 加载数据,我不需要编写该方法吗?例如这里? +(instancetype)levelWithNum:(int)levelNum { NSString* fileName = [NSString stringWithFormat:@"EachLevel%i.plist", levelNum]; NSString* levelPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; NSDictionary* levelDict = [NSDictionary dictionaryWithContentsOfFile:levelPath]; NSAssert(levelDict, @"level config file not found"); LoadPlistLevel* lev = [[LoadPlistLevel alloc] init]; lev.everylevel = levelDict[@"everylevel"];返回 lev;}
  • 那个功能没问题。得到levelDict后,使用你传递的key获取value。
  • 我该怎么做?我将此添加到 viewDidLoad 只是为了检查 - 但图像没有显示。 codeUIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@/4[1].jpg",folder1]]]; [self.view addSubview:imageView];code
【解决方案2】:

这样做,

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];
}

当文件不存在时,您可能必须通过创建一个新文件来处理错误,否则这应该可以工作。

【讨论】:

    【解决方案3】:

    您正在以正确的方式存储路径,当您的图像位于应用程序包中时,只需在 plist 中存储带有扩展名的图像文件名,以获取更多参考,您可以在 plist 中定义键名而不是“item1”、“item2”。

    现在进入实际问题,如何从 plist 访问图像

    第 1 步:从应用程序包中读取您的 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”加载图像,然后编写以下代码

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

    对于“第 6 项”

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多