您的位置:首页 > 移动开发 > IOS开发

iOS中,文件相对路径、绝对路径切换

2014-12-10 13:12 274 查看
最新更新了APP之后,发现了一个问题,上传附件的时候一直报错 No such file or directory。 但是,文件的的确确就在Documents目录下好好躺着。奇怪奇怪。

仔细对比之后,发现,生成附件的时候,绝对地址是:

/var/mobile/Containers/Data/Application/44C5E71D-B418-4FD3-A82A-45404C19465F/Documents/58961732/58961732_20141210003945072_Q2_1.jpg


其中Application后面有一串符号,表示当前是哪个APP。这个符号每次安装都不一样。

所以当我使用1.05版本,生成的附件,把地址放到数据库中。下次使用1.06的版本,上传附件,按照这个地址去找附件的时候,的确找不到了。

/var/mobile/Containers/Data/Application/74119C27-0ADF-4DB7-AE7D-705AB964B94C/Documents/58961732/58961732_20141210003945072_Q2_1.jpg

然后,无意中看到了这篇文章(第24), 里面讲到了扩展路径。原来通过扩展路径可以自由的切换绝对路径和相对路径。

NSString *Path = @"~/NSData.txt";

NSString *absolutePath = [Path stringByExpandingTildeInPath];

NSLog(@"absolutePath:%@",absolutePath);

NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);

运行结果:
2014-12-10 10:08:16.494 Flysurvey[2807:1377884] absolutePath:/var/mobile/Containers/Data/Application/B268916C-BDFF-4550-8A1D-1C88478921E4/NSData.txt
2014-12-10 10:08:16.498 Flysurvey[2807:1377884] Path:~/NSData.txt

另外,经过测试发现,stringByAbbreviatingWithTildeInPath这个方法,会根据当前的APP符号去缩短,所以,如果是上一版本的APP符号,是没有办法缩短的。

例如:

NSString *absolutePath = @"/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt";
NSString *path = [[absolutePath stringByAbbreviatingWithTildeInPath] copy];

NSLog(@"absolutePath:%@",absolutePath);

NSLog(@"Path:%@",path);

NSLog(@"hehe:%@ ",[path stringByExpandingTildeInPath] );

NSString *path2 = @"~/aa.txt";
NSLog(@"aa: %@", [path2 stringByExpandingTildeInPath]);

结果:
2014-12-10 10:20:15.637 Flysurvey[2886:1382167] absolutePath:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt
2014-12-10 10:20:15.640 Flysurvey[2886:1382167] Path:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt
2014-12-10 10:20:15.641 Flysurvey[2886:1382167] hehe:/var/mobile/Containers/Data/Application/AC672ACB-1BAB-4A6B-86DB-09E85FE3074F/NSData.txt
2014-12-10 10:20:15.641 Flysurvey[2886:1382167] aa: /var/mobile/Containers/Data/Application/D880650F-CF70-4990-AB05-86608CDECE93/aa.txt


可以看到,当前是D88开头的,上一次AC6开头的并没有被转换成功。依然是绝对路径。
为了兼容以前已经按照绝对路径保存的情况,所以还要自己截取Documents后面的路径,然后根据当前的APP,去转换正确的绝对路径。

于是,项目代码作下修改:
#import "Attachment.h"
#import "ZYFileUtil.h"
#import "ZYMyLog.h"

@implementation Attachment

@dynamic answer_index;
@dynamic answer_name;
@dynamic attachment_name;
@synthesize attachment_path = _attachment_path;
@dynamic attachment_type;
@dynamic project_id;
@dynamic upload_status;
@dynamic upload_time;

-(NSString *)attachment_path{

NSString *attachment_path_copy = [_attachment_path copy];

//2014-12-10 此版本(1.05.3)获取路径时,要处理以前绝对路径的情况。同时要兼容以后版本相对路径的情况。
if ([attachment_path_copy rangeOfString:@"~"].location == NSNotFound) {
//绝对路径,去掉Documents前面的部分,增加当前的base路径
NSInteger documentIndex = [attachment_path_copy rangeOfString:@"/Documents"].location;
NSMutableString *tempPath = [[attachment_path_copy substringFromIndex:documentIndex] mutableCopy];
[tempPath insertString:@"~" atIndex:0];
attachment_path_copy = [NSString stringWithFormat:@"%@",tempPath];
}

NSString *finalPath = [attachment_path_copy stringByExpandingTildeInPath];
[[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"读取数据库附件地址为: %@, 处理后的地址为%@", _attachment_path, finalPath]];
return finalPath;
}

-(void)setAttachment_path:(NSString *)attachment_path{
//2014-12-10 此版本(1.05.3)之后,存进去的都是相对路径
_attachment_path = [attachment_path stringByAbbreviatingWithTildeInPath];
[[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"原附件地址为:%@, 附件地址入库内容为: %@", attachment_path, _attachment_path]];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息