我有一个项目,其资源中包含图像。 例如:image1yellow.png,image2red.png,image3green.png ... 这些图像的数量可能不同,但我的应用程序必须知道数字和他们的名字。所以我想通过搜索资源从资源中收集这些图像...... 标题的'图像'部分是恒定的,并且在那之后有一个数字。标题的最后部分总是一个颜色名称(so..variable strings)>>> image + 3 + green = image3green。 我想,不知何故,我可以用这些指标分析搜索...搜索资源中的图像
搜索资源中的图像
回答
编辑
其实,一个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:
既然你知道文件名的结构,用一组给定的变量创建一个新的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}
,依此类推......
是的!我知道文件名的结构。它始终以'图片'标签开始,但是此部分后面的数字和颜色名称未知...... – madG
对于我在编辑中提到的问题存在明显的问题。我明天会添加它。 –
我编辑的代码更加可靠。希望这可以帮助! –
- 1. 如何从项目资源槽图像框中搜索图像
- 2. 在源内搜索图像
- 3. 检索资源名称是变量的图像资源
- 4. WinForms中的图像资源
- 5. android中的图像资源
- 6. FHIR - 搜索所有资源
- 7. 从资源文件夹检索图像
- 8. 如何从XML资源中的矢量图像检索位图
- 9. WPF图像资源
- 10. ImageView的图像资源
- 11. 图像资源的存在
- 12. 图像资源的位置
- 13. 图像视图设置图像资源
- 14. 在java中探索图像处理的好资源
- 15. 如何检索服务器中的图像资源位置
- 16. SetIconImage从资源中的图像
- 17. 调整CQ5中的图像资源
- 18. 保护类库中的图像资源
- 19. 访问项目资源中的图像?
- 20. 对象数组中的图像资源
- 21. 从资源管理器搜索框
- 22. 搜索/列出WPF资源文件?
- 23. 轨道搜索嵌套资源
- 24. eclipse搜索资源包括jar文件
- 25. 按内部参考搜索FHIR资源
- 26. php:显示图像资源
- 27. WinForms UserControl图像资源
- 28. ASP.net Web资源与图像
- 29. 设置图像资源
- 30. Android:OutOfMemoryError加载图像资源
从这里开始获取文件列表,然后执行数组搜索。 http://stackoverflow.com/questions/7900879/how-to-list-all-folders-and-their-subdirectories-files-in-iphone-sdk – trumpetlicks
如果任何提供的解决方案有助于解决此问题,请要么标记已经解决问题的解决方案和/或提高你发现有用的任何答案。谢谢! –