2013-02-04 56 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

我編輯的代碼更加可靠。希望這可以幫助! –