iPhone - file properties

烂漫一生 提交于 2019-12-02 12:19:42

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
    }

}

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]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!