代码之家  ›  专栏  ›  技术社区  ›  iOS

iPhone-文件属性

  •  0
  • iOS  · 技术社区  · 15 年前

    我正在创建一个应用程序,使iphone作为pendrive工作,以便于文件共享。

    在第一阶段中,我在一个目录中有一些文件(png、pdf、jpg、zip),并使它们以可变数组的形式显示在tableview中。它显示在tableView中,如下所示,

    .DS_Store
    .localized
    gazelle.pdf
    Hamburger_sandwich.jpg
    IITD TAJ Picture 028_jpg.jpg
    iya_logo_final_b&w.jpg
    manifesto09-eng.pdf
    RSSReader.sql
    SimpleURLConnections.zip
    SQLTutorial
    

    .DS_Store
    .localized
    gazelle
    Hamburger_sandwich
    IITD TAJ Picture 028_jpg
    iya_logo_final_b&w
    manifesto09-eng
    RSSReader
    SimpleURLConnections
    SQLTutorial
    

    在第二个阶段中,我有一个detailedViewController,它将显示文件的详细视图,如

    1. 文件大小
    2. 如果是图像,则应在imageView中打开
    3. 如果是一首歌,它应该播放它

    所以我需要重新获取文件路径、文件类型、文件大小等属性。。每个文件。如果可能的话,请给我一个指导。我也不知道如何将可变数组转换为NSString并调用stringByDeletingPathExtension之类的方法。请帮帮我。如果需要,我甚至可以发送我的源代码。如果可能的话,请给我一些示例代码和教程。

    2 回复  |  直到 15 年前
        1
  •  0
  •   nacho4d    15 年前

    这应该有效:)

    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
        }
    
    }
    
        2
  •  0
  •   itsaboutcode    15 年前

    我希望您在NSURL对象中有文件名,如果有,那么您可以使用以下代码来获取文件名,并可以从字符串中删除文件扩展名。

    NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:@"."];
    NSLog(@"%@",[fileName objectAtIndex:0]);