ios iphone - 如何检查文件是目录、音频、视频还是图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17145844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:58:19  来源:igfitidea点击:

iphone - how to check if the file is a directory , audio , video or image?

iphoneiosnsfilemanager

提问by Subbu

Following my program shows contents of Documents directory and is displayed in a tableView . But the Documents directory contains some directories , some audio , some video , and some images etc .

按照我的程序显示 Documents 目录的内容并显示在 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]);

So , I want to check if file is directory then it directory image can be shown in the cell's imageView . Same for audio , video and image .

所以,我想检查文件是否是目录,然后它的目录图像可以显示在单元格的 imageView 中。音频、视频和图像也是如此。

I searched in NSFileManager classReference , but dint get a solution .

我在 NSFileManager classReference 中搜索,但没有得到解决方案。

How to do this ?

这该怎么做 ?

回答by Bhavin

Sample Code :

示例代码:

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
    }
}


You can also use Uniform Type Identifiersas explained by Bavarioushere.

您还可以使用统一类型标识符,Bavarious here 所述

Sample Code :

示例代码:

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);

回答by William Falcon

Look at the NSURLclass. You can for example see if the url is a file url by saying isFileUrl.

NSURL班级。例如,您可以通过说 来查看 url 是否是文件 url isFileUrl

Additionally you can get the last component of the url (usually the file name) by doing:

此外,您可以通过执行以下操作获取 url 的最后一个组件(通常是文件名):

NSString *filename = [[url path] lastPathComponent];

These can help you do what you need, but more importantly look at the NSURLdocumentation.

这些可以帮助你做你需要的,但更重要的是查看NSURL文档。

回答by Hugo the Lee

Just in case of image file type

以防万一图像文件类型

-(BOOL)isImageSource:(NSURL*)URL
{
    CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)URL, NULL);
    NSString *UTI = (__bridge NSString*)CGImageSourceGetType(source);
    CFRelease(source);

    CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
    NSArray *array = (__bridge NSArray*)mySourceTypes;
    CFRelease(mySourceTypes);

    return [array containsObject:UTI];
}

回答by Evdzhan Mustafa

For any Swift lovers, coming to this question trying to check if a file is a directory here you go:

对于任何 Swift 爱好者,来到这个问题试图检查文件是否是一个目录:

///
/// Check if NSURL is a directory
///
internal func fileIsDir(fileURL: NSURL) -> Bool {
    var isDir: ObjCBool = false;
    fileManager.fileExistsAtPath(fileURL.path!, isDirectory: &isDir)
    return Bool(isDir);
}

///
/// Check if a path is a directory
///
internal func fileIsDir(path: String) -> Bool {
    var isDir: ObjCBool = false;
    fileManager.fileExistsAtPath(path, isDirectory: &isDir)
    return Bool(isDir);
}

(Note that I tried doing return isDir as Boolbut it didn't work. But the above Bool initiation seems to work.)

(请注意,我尝试这样做,return isDir as Bool但没有奏效。但上面的 Bool 启动似乎有效。)

回答by dougzilla

Updated Evdzhan's answer for Swift 4.0:

更新了 Evdzhan 对 Swift 4.0 的回答:

//
// Check if NSURL is a directory
//
func fileIsDir(fileURL: NSURL) -> Bool {
    var isDir: ObjCBool = false;
    FileManager.default.fileExists(atPath: fileURL.path!, isDirectory: &isDir)
    return isDir.boolValue
}

//
// Check if a path is a directory
//
func fileIsDir(path: String) -> Bool {
    var isDir: ObjCBool = false;
    FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
    return isDir.boolValue
}