iPhone - file properties

前端 未结 2 1223
后悔当初
后悔当初 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: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
        }
    
    }
    

提交回复
热议问题