保存数据 - 在模拟器上有效,但在设备上无效

发布于 2024-08-30 04:11:13 字数 1397 浏览 7 评论 0原文

我使用 NSData 在我的应用程序中保存图像。我像这样保存图像(在我的应用程序代理中):

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"Saving data");

    NSData *data = UIImagePNGRepresentation([[viewController.myViewController myImage]image]);
    //Write the file out!

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

    [data writeToFile:path_to_file atomically:YES];
    NSLog(@"Data saved.");
}

然后像这样加载它(在我的视图控制器中,在 viewDidLoad 下):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

if([[NSFileManager defaultManager] fileExistsAtPath:path_to_file])
{
    NSLog(@"Found saved file, loading in image");
    NSData *data = [NSData dataWithContentsOfFile:path_to_file];

    UIImage *temp = [[UIImage alloc] initWithData:data];
    myViewController.myImage.image = temp;
    NSLog(@"Finished loading in image");
}

此代码每次在模拟器上都有效,但在设备,它似乎永远无法加载回图像中。我很确定它会保存,但我看不到文件系统。

我在做什么奇怪的事吗?模拟器访问其目录的方式是否与设备不同?

谢谢!

I use NSData to persist an image in my application. I save the image like so (in my App Delegate):

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"Saving data");

    NSData *data = UIImagePNGRepresentation([[viewController.myViewController myImage]image]);
    //Write the file out!

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

    [data writeToFile:path_to_file atomically:YES];
    NSLog(@"Data saved.");
}

and then I load it back like so (in my view controller, under viewDidLoad):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

if([[NSFileManager defaultManager] fileExistsAtPath:path_to_file])
{
    NSLog(@"Found saved file, loading in image");
    NSData *data = [NSData dataWithContentsOfFile:path_to_file];

    UIImage *temp = [[UIImage alloc] initWithData:data];
    myViewController.myImage.image = temp;
    NSLog(@"Finished loading in image");
}

This code works every time on the simulator, but on the device, it can never seem to load back in the image. I'm pretty sure that it saves out, but I have no view of the filesystem.

Am I doing something weird? Does the simulator have a different way to access its directories than the device?

Thanks!

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

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

发布评论

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

评论(1

离笑几人歌 2024-09-06 04:11:13

啊哈!搞定了!

我写这篇文章是为了其他人不会坐在那里盯着代码,想知道为什么它不起作用:)

有问题的一行是这一行:

NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

它应该看起来像这样:

NSString *path_to_file = [documentsDirectory stringByAppendingPathComponent:@"lastimage.png"];

这样做的原因是,没有它,您会得到类似 /Files/Documentslastimage.png 而不是 /Files/Documents/lastimage.png 的内容,因为附加的路径组件会为您处理斜杠。我希望这对将来的其他人有帮助!

Aha! Nailed it!

I'm writing this so someone else pulling their hair doesn't sit there staring at code, wondering why it doesn't work :)

The problematic line is this one:

NSString *path_to_file = [documentsDirectory stringByAppendingString:@"lastimage.png"];

It should look like this:

NSString *path_to_file = [documentsDirectory stringByAppendingPathComponent:@"lastimage.png"];

The reason for this is, without it, you'd get something along the lines of /Files/Documentslastimage.png instead of /Files/Documents/lastimage.png, as the path component appending takes care of the slashes for you. I hope this helps someone else in the future!

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