2

在我的应用程序中,我将视频和照片保存在文档目录中,并将照片和视频 url 路径存储在 sqlite 中。在更新(升级)iPhone 应用程序时,所有文档目录中的照片和视频都会被删除。但是存储在sqlite中的其他参数没有被删除。如何在更新应用程序时保留文件?

-(BOOL)saveImageToDocumentDirectory:(UIImage*)imgToBeSaved { BOOL flag = NO;

strPhotourl=nil;

NSData* imgData = UIImageJPEGRepresentation(imgToBeSaved, 1.0);

// NSData *imgData1=UIImagePNGRepresentation(imgToBeSaved);

// Create a new UUID
CFUUIDRef uuidObj = CFUUIDCreate(nil);

// Get the string representation of the UUID
NSString *strImageName = (__bridge NSString*)CFUUIDCreateString(nil, uuidObj);

CFRelease(uuidObj);

//Call Function to save image to document directory

NSArray* arrAllDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString* strDocumentDirectory = [arrAllDirectories objectAtIndex:0];

NSFileManager* fileManager = [NSFileManager defaultManager];

NSString *fullPath = [strDocumentDirectory stringByAppendingPathComponent:[strImageName stringByAppendingString:@".png"]];

strPhotourl=fullPath ;// strPhotourl store in sqlite db table two show images in  tableview

//    NSLog(@"Image in strphotolocation==%@",imagePath);

// NSLog(@"fullpath---%@",fullPath);

if(![fileManager fileExistsAtPath:fullPath])
    flag =     [fileManager createFileAtPath:fullPath contents:imgData attributes:nil];
else
    flag = NO;
return flag;

}

4

2 回答 2

7

原因不是因为删除了任何文件,而是因为 iOS 应用程序所在的文件夹在升级过程中确实发生了变化。存储应用程序的文件夹使用 GUID 命名,升级时可能会发生变化。

您不应将完整的 URL/PATH 存储到 Documents 文件夹中的任何文件中,因为不能保证在升级后保留。

应用程序更新期间保存的文件

当用户下载应用更新时,iTunes 会将更新安装在新的应用目录中。然后它将用户的数据文件从旧安装移动到新的应用程序目录,然后再删除旧安装。以下目录中的文件保证在更新过程中被保留:

  • 申请_首页/文件
  • 应用_首页/库

尽管其他用户目录中的文件也可能被移动,但您不应依赖它们在更新后存在。

https://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html

如果您已经在用户手机上部署了应用程序,那么您可以做的是当您从 sqlite 读取 url 时,删除前导路径(例如:/var/mobile/Applications/---guid---/Documents)和然后在使用之前重新保存条目。

于 2013-07-09T11:17:28.453 回答
4

不要将整个路径保存到 sqlite,只需保存您存储在本地的文件名,以便在获取时获取当前目录路径并附加来自 sqlite 的 fileName。这也将在您升级应用程序时解决。

于 2013-07-09T11:18:49.827 回答