获取本地文件列表及预览文件

本文介绍如何在iOS应用中展示类似QQ的本地文件列表,并实现文件预览功能。通过使用NSFileManager获取Documents目录下的文件和文件夹,然后用tableView显示,通过文件选择器预览文件。详细代码和示例项目可在优快云和GitHub找到。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如同QQ中收到的文件一样,支持用其他应用打开文件

思路:先用tableView加载显示本地文件,在用文件选择器实现预览文件的效果。。

核心代码如下:

NSFileManager *fileManager = [NSFileManager defaultManager];
    //在这里获取应用程序Documents文件夹里的文件及文件夹列表
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docDirs = [paths objectAtIndex:0];
    NSError *error = nil;
    
    //_dataFileArray是包含有该文件夹下所有文件的文件名及文件夹名的数组
    _dataFileArray = [fileManager contentsOfDirectoryAtPath:docDirs error:&error];
    fileList = [NSMutableArray array];
    XCPFileObject *fileObj;
    for (NSString *name in _dataFileArray) {
        fileObj = [[XCPFileObject alloc] init];
        fileObj.fileName = name;
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:[docDirs stringByAppendingPathComponent:name] error:nil];
        fileObj.filemTime = [fileAttributes objectForKey:@"NSFileCreationDate"];
        fileObj.fileSize = [[fileAttributes objectForKey:@"NSFileSize"] integerValue];
        fileObj.fileOwner = [fileAttributes objectForKey:@"NSFileGroupOwnerAccountName"];
        
        [fileList addObject:fileObj];
    }
    
    //列出给定一个文件夹里的所有子文件夹名
    NSMutableArray *dirArray = [[NSMutableArray alloc] init];
    BOOL isDir = NO;
    
    for (NSString *file in _dataFileArray) {
        NSString *path = [docDirs stringByAppendingPathComponent:file];
        [fileManager fileExistsAtPath:path isDirectory:(&isDir)];
        if (isDir) {
            [dirArray addObject:file];
        }
        isDir = NO;
    }

原文Dome:http://download.youkuaiyun.com/detail/xcp_123/9735844

github地址:https://github.com/XiaCuiPing/XCP_FileView

在github的请点一下star支持哦,谢谢!

注:本文版权归作者所有,转载请注明出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值