0

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

在第二阶段,我有一个详细视图控制器,它显示文件的详细视图,例如

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

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

4

2 回答 2

0

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

NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:@"."];
NSLog(@"%@",[fileName objectAtIndex:0]);
于 2010-11-09T14:28:30.997 回答
0

这应该可以工作:) 这将获取 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
    }

}
于 2010-11-09T15:04:56.733 回答