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

后端 未结 5 1289
长发绾君心
长发绾君心 2021-01-31 02:54

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 , an

相关标签:
5条回答
  • 2021-01-31 03:34

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

    ///
    /// 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 Bool but it didn't work. But the above Bool initiation seems to work.)

    0 讨论(0)
  • 2021-01-31 03:39

    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 Identifiers as explained by 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);
    
    0 讨论(0)
  • 2021-01-31 03:40

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

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

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

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

    0 讨论(0)
  • 2021-01-31 03:43

    Updated Evdzhan's answer for 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
    }
    
    0 讨论(0)
  • 2021-01-31 03:45

    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];
    }
    
    0 讨论(0)
提交回复
热议问题