保存数据 - 在模拟器上有效,但在设备上无效
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊哈!搞定了!
我写这篇文章是为了其他人不会坐在那里盯着代码,想知道为什么它不起作用:)
有问题的一行是这一行:
它应该看起来像这样:
这样做的原因是,没有它,您会得到类似 /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:
It should look like this:
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!