iPhone - file properties

前端 未结 2 1221
后悔当初
后悔当初 2021-01-27 15:07

i m creating an application which makes the iphone work as a pendrive for easy file sharing purpose.

In the first stage, i have some files(png, pdf, jpg, zip) in a direc

相关标签:
2条回答
  • 2021-01-27 15:43

    I hope you will have the file name in NSURL object, if so then you can use the following code to get just the file name and can remove the file extension from the string.

    NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:@"."];
    NSLog(@"%@",[fileName objectAtIndex:0]);
    
    0 讨论(0)
  • 2021-01-27 15:50

    This should work:) This will get all files in a directory in a NSString *parentDirectory, get its size, if image do something otherwise it assumes is a sound file

    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *filePaths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }   
    for (NSString *filePath in filePaths) {
        //filename without extension
        NSString *fileWithoutExtension = [[filePath lastPathComponent] stringByDeletingPathExtension];
    
        //file size
        unsigned long long s = [[fm attributesOfItemAtPath:[parentDirectory stringByAppendingPathComponent:filePath] 
                                      error:NULL] fileSize];
    
        UIImage *image = [UIImage imageNamed:[parentDirectory stringByAppendingPathComponent:filePath];];
        //if image...
        if(image){
            //show it here
        }
        else{
            //otherwise it should be music then, play it using AVFoundation or AudioToolBox
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题