【问题标题】:iPhone - file propertiesiPhone - 文件属性
【发布时间】:2011-05-07 06:49:20
【问题描述】:

我正在创建一个应用程序,它使 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

我只想显示文件名,不想显示扩展名。我知道可以在 NSFileManager 中提取文件的扩展名。但是我不知道怎么做。请帮我让我的表格视图看起来像这样

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

在第二阶段,我有一个 detailViewController,它可以显示文件的详细视图,例如

  1. 文件大小
  2. 文件类型
  3. 如果是图片,应该在imageView中打开
  4. 如果是歌曲,应该播放它

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

【问题讨论】:

    标签: iphone uitableview nsfilemanager


    【解决方案1】:

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

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

    【讨论】:

    • 没有。我在文档目录中有文件。我根本不使用网址。我需要在没有文件扩展名的情况下在 tableView 中显示我的目录中的内容.. 谢谢你的回复..
    【解决方案2】:

    这应该工作:) 这将获取 NSString *parentDirectory 中的目录中的所有文件,获取其大小,如果图像执行其他操作,则假定它是声音文件

    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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多