My app uses iTunes file sharing. The user can add files to the Documents directory.
I have to read these files, but make sure that they are only images, not text files or other โjunkโ.
How can I iterate over files in a directory and only recognize those that are images?
I guess I would have to do it 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 reason. I will need HUUUUUUGE if-clause to catch all possible types of image files, and there are DOSES.
Is there a smarter way to really collect all image files, regardless of whether they are .png, .bmp, .jpg, .jpeg, .jpeg2000, .tiff, .raw, .etc?
I remember a little that there were some file attributes that talked about the general type of file. I believe that there is some kind of abstract image. But maybe there is an even better method?
source share