How to iterate over all image files in a directory?

My app uses iTunes File Sharing. The user can add files to the Documents directory.

I must read these files but make sure they're only images and not text files or other "garbage".

How can I iterate over the files in a directory and recognize only those which are images?

I assume that I would have to do it somehow like this:

NSMutableArray *retval = [NSMutableArray array];

NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirPath error:&error];
if (files == nil) {
    // error...
}

for (NSString *file in files) {
    if ([file.pathExtension compare:@"png" options:NSCaseInsensitiveSearch] == NSOrderedSame) {        
        NSString *fullPath = [documentsDirPath stringByAppendingPathComponent:file];
        [retval addObject:fullPath];
    }
}

But this is bad for some reasons. I would need a HUUUUUGE if-clause to catch all possible image file types, and there are DOZENS of them.

Is there a more intelligent way to really collect all image files, no matter if .png, .bmp, .jpg, .jpeg, .jpeg2000, .tiff, .raw, .etc?

I slightly remember that there were some kind of file attributes that told the general type of file. There was some abstract image key I believe. But maybe there's an even better method?

6
задан dontWatchMyProfile 25 May 2011 в 18:50
поделиться