我有一個項目,其資源中包含圖像。 例如: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
如果任何提供的解決方案有助於解決此問題,請要麼標記已經解決問題的解決方案和/或提高你發現有用的任何答案。謝謝! –