简体   繁体   中英

How to get all images in the project folder that start with a specific string

I am quite new in iOS development and Objective-C and I would appreciate some help.

I created a Group folder in Xcode where I placed all my images. All my images are inside the project folder.

I want to iterate through the Xcode folder routineIcons and get all Images that have b- in their name and put their name and extension into an array.

Could anyone point me in the right direction on how to do this? bah.. is it possible??

在此输入图像描述

You can use this snipped:

 NSMutableArray *result = [NSMutableArray array];
 [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    NSString *path = [obj lastPathComponent];
    if ([path hasPrefix:@"b-"]) {
        [result addObject:path];
    }
}];

At runtime, the routineIcons folder won't exist. All of those images, along with all other resources, will end up in the app's resource bundle.

What you can do is get the path to the resource bundle then use NSFileManager to get all of the files in the folder. You can then ignore any that don't start with "b-".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM