Как читать изображения из NSMainBundle в iOS

У меня есть структура, подобная следующей, в моем проекте Xcode.

введите здесь описание изображения

Мне нужно прочитать все изображения из проекта и сохранить их в массиве.

Я использовал следующий код из некоторых других сообщений StackOverflow, но он не работает.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Images"ofType:@""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
    if([[filename pathExtension] isEqualToString:@"png"]){
        [imageFolder addObject:filename];
    }
}
NSLog(@"Files in the folder %@",imageFolder);

Я был бы признателен, если бы кто-нибудь помог мне завершить это.


person Peer Mohamed Thabib    schedule 05.08.2014    source источник
comment
См. мой ответ здесь получить доступ ко всем изображениям в xcassets одновременно"> stackoverflow.com/questions/24389720/   -  person iphonic    schedule 05.08.2014


Ответы (4)


- (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)subpath даст вам нужный массив.

NSArray *imagesArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images"];
person The dude    schedule 05.08.2014

просто получить все изображения в массив

NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil];
person Retro    schedule 05.08.2014

Вы можете попробовать ниже

NSArray *pathArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];

[pathArray enumerateObjectsUsingBlock:^(NSString *pathString, NSUInteger idx, BOOL *stop) {

    UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:pathString];
}];
person Tapas Pal    schedule 05.08.2014

Пожалуйста, используйте этот код, надеюсь, он сработает.

    NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"images"];
    NSError * error;
    NSArray * images =  [[NSFileManager defaultManager]
                  contentsOfDirectoryAtPath:dirPath error:&error];
person Sreejith Bhatt    schedule 05.08.2014