0

I have a project, which contains images in its resources. For example: image1yellow.png, image2red.png, image3green.png... The number of these images could be different, but my app has to know the number and their names. So I want to collect these images from the resources by searching for them... The 'image' part of the title is constant and right after that there is a number. The final part of the title is always a color name (so..variable strings) >>> image+3+green=image3green. I think somehow I can search with these criterias...

4

2 回答 2

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},依此类推...

于 2013-02-04T21:39:51.053 回答
0

编辑

实际上,NSBundle 有一个更方便的方法:

URLsForResourcesWithExtension:子目录:inBundleWithURL:

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

要获取以图像开头的资源,您可以使用:

    NSArray * array = [NSBundle URLsForResourcesWithExtension:@"png" subdirectory:nil inBundleWithURL:[[NSBundle mainBundle] bundleURL]];
    [数组 indexOfObjectsPassingTest:^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:包括PropertiesForKeys:选项:错误:

于 2013-02-04T21:30:58.230 回答