iphone – 如何检查文件是否是目录,音频,视频或图像?

下面我的程序显示文档目录的内容,并显示在tableView中。
但是Documents目录包含一些目录,一些音频,一些视频和一些图像等。

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
fileType = @"" ;
NSString *subDirectoryPath = [docsDir stringByAppendingPathComponent:fileType];
NSLog(@"path : %@",subDirectoryPath);
files  = [[NSMutableArray alloc] initWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtPath:subDirectoryPath error:nil]];
NSLog(@"files ::::: %@ ",[files description]);

所以,我想检查如果文件是目录,那么它的目录图像可以显示在单元格的imageView。与音频,视频和图像相同。

我在NSFileManager classReference中搜索,但dint得到一个解决方案。

如何做到这一点?

示例代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directory = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
BOOL isDirectory;
for (NSString *item in directory){
    BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:item isDirectory:&isDirectory];
    if (fileExistsAtPath) {
       if (isDirectory)
       {
           //It's a Directory.
       }
    }
    if ([[item pathExtension] isEqualToString:@"png"]) {
       //This is Image File with .png Extension
    }
}

您也可以使用Uniform Type Identifiers,如Bavarious here所述。

示例代码:

NSString *file = @"…"; // path to some file
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");

CFRelease(fileUTI);
http://stackoverflow.com/questions/17145844/iphone-how-to-check-if-the-file-is-a-directory-audio-video-or-image

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 如何检查文件是否是目录,音频,视频或图像?