Количество изображений в моем [[NSBundle mainBundle] bundlePath] - PullRequest
6 голосов
/ 08 сентября 2011

Как узнать количество изображений в [[NSBundle mainBundle] bundlePath] в моей папке ....

Например: / Пользователи / Наг / Библиотека / Поддержка приложений / iPhone Simulator / 4.2 / Приложения / 4409487C-1035-4329-B38A-B6F21E00FC63 / MakeUp_IPhone.app / " MyFolder "

1 Ответ

10 голосов
/ 08 сентября 2011

http://www.ericd.net/2009/06/iphone-getting-images-from-your-bundle.html

http://www.cocoabuilder.com/archive/cocoa/239638-number-of-images-in-main-bundle-folder-iphone.html

Проверьте вышеуказанные ссылки. Они могут предоставить решение вашей проблемы.
Вот код для вас:

    totalCount = 0;
    NSArray *d = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil];
    for(NSString *s in d)
    {
       if([[s lastPathComponent] hasPrefix:@"image_"]){
       totalCount++;
       }
    }

Для получения изображений из определенной папки, см. Это: Количество изображений

    NSEnumerator *iter = [[NSFileManager defaultManager] directoryEnumeratorAtPath:[[NSBundle mainBundle] bundlePath]];
    int count = 0; // number of images
    for (NSString *path in iter) { // iterate through all files in the bundle
         if ([[path pathExtension] isEqualToString:@"png"]) { // test if the extension is "png"
            count++; // increment the number of images
            UIImage *img = [UIImage imageWithContentsOfFile:path];
            // do other things with the image
        }
    }

См. Также:

// create the route of localDocumentsFolder
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//first use the local documents folder
NSString *docsPath = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
//then use its bundle, indicating its path
NSString *bundleRoot = [[NSBundle bundleWithPath:docsPath] bundlePath];
//then get its content
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
// this counts the total of jpg images contained in the local document folder of the app
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.JPG'"]];
// in console tell me how many jpg do I have
NSLog(@"numero de fotos en total: %i", [onlyJPGs count]);
// ---------------
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...