iPhone - 文件属性

发布于 2024-10-01 17:23:26 字数 927 浏览 2 评论 0原文

我正在创建一个应用程序,使 iPhone 可以用作随身碟,以方便文件共享。

在第一阶段,我在目录中有一些文件(png,pdf,jpg,zip),我让它们以可变数组的形式显示在表格视图中。它显示在 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

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

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

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

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 directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,

.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

I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this

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

In the second stage i have a detailedViewController which takes displays the detailed view of the file like

  1. file size
  2. file type
  3. if it is a image, it should open in imageView
  4. if it is a song, it should play it

So i need to retrive the properties like filePath, fileType, fileSize.. of each files. Please guide me with a tutorial if possible. I too do not have any idea how to convert a mutableArray to a NSString and call the methods like stringByDeletingPathExtension. Please help me. I can even send my source code if needed. If possible, guide me with some example codes and tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-10-08 17:23:26

这应该有效:)
这将获取 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
    }

}

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
    }

}
掀纱窥君容 2024-10-08 17:23:26

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

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

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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文