2013-02-04 57 views
0

我有一个项目,其资源中包含图像。 例如:image1yellow.png,image2red.png,image3green.png ... 这些图像的数量可能不同,但我的应用程序必须知道数字和他们的名字。所以我想通过搜索资源从资源中收集这些图像...... 标题的'图像'部分是恒定的,并且在那之后有一个数字。标题的最后部分总是一个颜色名称(so..variable strings)>>> image + 3 + green = image3green。 我想,不知何故,我可以用这些指标分析搜索...搜索资源中的图像

+0

从这里开始获取文件列表,然后执行数组搜索。 http://stackoverflow.com/questions/7900879/how-to-list-all-folders-and-their-subdirectories-files-in-iphone-sdk – trumpetlicks

+0

如果任何提供的解决方案有助于解决此问题,请要么标记已经解决问题的解决方案和/或提高你发现有用的任何答案。谢谢! –

回答

0

编辑

其实,一个NSBundle有一个更convienient方法:

URLsForResourcesWithExtension:subdirectory:inBundleWithURL:

有了这个,你可以得到所有的数组你的png资源,然后用它来确定你想要的图像。

要获得ressources开始像那么你可以使用:

 
    NSArray * array = [NSBundle URLsForResourcesWithExtension:@"png" subdirectory:nil inBundleWithURL:[[NSBundle mainBundle] bundleURL]]; 
    [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { 
     return [obj isKindOfClass:NSURL.class] && [[((NSURL *) obj) resourceSpecifier] hasPrefix:@"image"]; 
    }]; 

当然,这是检索它的最佳方式,我几乎可以肯定你有一些简单的逻辑来获得图像名称的结尾,你可能会有更好的算法(也许你可以对数组进行排序并使用枚举器)。此外,您可能希望在静态字段中保留最终结果(例如,以图像开头的所有资源),以便更快地检索它们,而不是每次都重做所有工作。

基本上,你必须根据你真正想要的东西来做剩下的实现(或者给出你的逻辑的例子来得到最终的图像)。

原始响应

我不知道我知道你想要什么,但我认为这是类似的东西:

 
    NSArray * ressources = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error:&error]; 

你也可以考虑

contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

+0

好的,谢谢!用你最初的回复(NSArray * ressources ...)我可以列出项目资源。这是一个改进,但我仍然不知道如何只列出启动'图像'标签的图像。 – madG

+0

你可以在数组的结果中寻找这个 – Xval

+0

这将起作用,但它不能保证字符串图像的格式,后面是一个整数,后面是一个颜色。它只检查开始的单词是否是图像,文件类型是否是png。 –

0

既然你知道文件名的结构,用一组给定的变量创建一个新的NSString:

NSUInteger arbitraryNumber = 7; 
NSString *color = @"yellow"; 
NSString *extension = @"png"; 
NSString *imageName = [NSString stringWithFormat:@"image%d%@.%@", arbitraryNumber, color, extension]; 

或分配这些图像以在一个循环中的阵列...

编辑:手头的问题明确监督之后...

以下是使用正则表达式的递归溶液在文件名中获得所需的“短语”。我为你写了这篇文章并进行了测试。当然,这个东西有点棘手,所以如果你给它一个像“images23green.png”这样的文件名,它可能会失败。如果你担心这一点,你应该学习更多关于正则表达式的知识!

要执行此操作,请致电[self loadImages]

- (void)loadImages 
{ 
    NSDictionary *filenameElements = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"^(image)\\d+\\w+\\.\\w{3}$", @"^(image)", @"^\\d+", @"^\\w+", @"^\\w{3}", nil] forKeys:[NSArray arrayWithObjects:@"filename", @"image", @"number", @"color", @"fileExtension", nil]]; 
    NSString *string = @"image23green.png";  // Example file name 
    NSError *error = NULL; 
    error = [self searchForSubstring:@"image" inString:string withRegexFormatDictionary:filenameElements beginAtIndex:0]; 
} 

- (NSError *)searchForSubstring:(NSString *)key inString:(NSString *)givenString withRegexFormatDictionary:(NSDictionary *)filenameElements beginAtIndex:(NSInteger)index 
{ 
    NSError *error = NULL; 
    NSString *substring = [givenString substringFromIndex:index]; 

    NSString *regexPattern = [filenameElements objectForKey:key]; 
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexPattern options:NSRegularExpressionCaseInsensitive error:&error]; 
    NSArray *matches = [regex matchesInString:substring options:0 range:NSMakeRange(0, [substring length])]; 
    if ([matches count] > 0) 
    { 
     NSRange matched = [[matches objectAtIndex:0] rangeAtIndex:0]; 
     NSLog(@"matched: %@", [substring substringWithRange:matched]); 

     index = 0; 
     NSString *nextKey; 
     if  ([key isEqualToString:@"image"])   { nextKey = @"number"; index = matched.length; } 
     else if ([key isEqualToString:@"number"])  { nextKey = @"color"; index = matched.length; } 
     else if ([key isEqualToString:@"color"])   { nextKey = @"fileExtension"; index = matched.length + 1; } 
     else if ([key isEqualToString:@"fileExtension"]) { nextKey = nil; } 
     if (nextKey) 
      error = [self searchForSubstring:nextKey inString:substring withRegexFormatDictionary:filenameElements beginAtIndex:index]; 
    } 
    return error; 
} 

将该溶液接受带有任何长度的数值,任何颜色名称长度,将只接受字母字符(ⅰ一个文件名。即,不允许使用连字符)和一个3个字符长的文件扩展名。如果您希望范围为3到4个字符长度,则可以更换{3}{3-4},依此类推......

+0

是的!我知道文件名的结构。它始终以'图片'标签开始,但是此部分后面的数字和颜色名称未知...... – madG

+0

对于我在编辑中提到的问题存在明显的问题。我明天会添加它。 –

+0

我编辑的代码更加可靠。希望这可以帮助! –