我有一个应用程序,用于将用户的图像存储在app / documents文件夹中,我正在执行以下代码确定保存它的路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [[[[NSNumber alloc] initWithInteger:self.nextID] stringValue] stringByAppendingFormat:@".jpg"];
NSString *dirPath = [docs stringByAppendingFormat:@"/Photos/"];
imagePath = [dirPath stringByAppendingString:imageFilename];
它给了我一条类似于(在模拟器中)的路径: /var/mobile/Applications/C9C43CFD-6A5F-40CF-8BAE-20496B5A544A/Documents/Photos/1.jpg
我保存此路径以在需要时显示此图像。
我的问题是,当我在应用程序商店中发送更新时,在更新应用程序后,它无法再显示图像。 我的理解是在AppStore中更新应用程序时,Documents文件夹无法更改。
我可能的原因是它在Applications文件夹和Documentar之间的名称发生了变化,从AppStore更新应用程序版本是否正确?
有没有人有任何想法?有没有办法在本地测试这个应用程序更新?
由于
答案 0 :(得分:1)
您需要保存Documents
目录之后的相对路径,而不是绝对路径。应用程序的目录可以在更新期间更改,但应用程序沙箱中的结构不会更改。
此外,还有一种更好的方法来构建您的路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [NSString stringWithFormat:@"%d.jpg", self.nextID];
NSString *dirPath = [docs stringByAppendingPathComponent:@"Photos"];
imagePath = [dirPath stringByAppendingPathComponent:imageFilename];
答案 1 :(得分:0)
从App Store更新应用程序时,Documents目录的内容不变。应用程序包及其所有内容将替换为新版本,但“文档”文件夹是在应用程序版本之间存储内容的安全位置。