应用程序重新运行后,通过 WriteToFile 命令写入的图像丢失
Image written through WriteToFile command is missing after app rerun
我正在编写一个应用程序,它从我的服务器下载图像并将它们存储在本地以供离线查看。我用来在本地存储图像的代码如下所示。除了存储图像外,我还可以从路径(我单独存储)中读取图像。
然而,当我将我的代码从 Xcode 重新 运行 到 iPhone 时(没有从 phone 卸载应用程序),存储的图像文件将是失踪。这是预期的吗?当我的应用程序已在 App Store 上发布以及用户更新应用程序时,我会遇到类似的问题吗?当我更新 Xcode 中的一些代码时,当我重新 运行 它们时,有没有办法保持文件持久?
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"file.jpeg",]];
if (![imageData writeToFile:imagePath atomically:YES])
{
// Handling when there is an error
NSLog(@"Failed to store file at path %@",imagePath);
stored.text = @"";
}
else
{ // Handling when write is successful
NSLog(@"Imaged stored path is %@",imagePath);
stored.text = imagePath;
}
[imageView setImage:image];
读取图片的代码如下:
NSString *imagePathToRead = stored.text;
NSLog(@"Retrieve image from %@",imagePathToRead);
UIImage *image = [UIImage imageWithContentsOfFile:imagePathToRead];
[imageView setImage:image];
我在这里发布了 rmaddy 的答案,它解决了我面临的问题。我在这里发帖是为了让其他遇到类似问题的人受益。
对于存储部分:
stored.text 不应存储完整路径:
stored.text = imagePath;
相反,我应该只存储@"file.jpeg"。
阅读部分:
当我想读取路径而不是下面的代码时,我应该总是重新生成下面的完整路径:
NSString *imagePathToRead = stored.text;
更正后的代码是:
NSString *imagePathToRead = stored.text;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:imagePathToRead]];
NSLog(@"Retrieve image from %@",imagePath);
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
这解决了问题,即使通过 Xcode 重新安装应用程序也可以读取图像。
我正在编写一个应用程序,它从我的服务器下载图像并将它们存储在本地以供离线查看。我用来在本地存储图像的代码如下所示。除了存储图像外,我还可以从路径(我单独存储)中读取图像。
然而,当我将我的代码从 Xcode 重新 运行 到 iPhone 时(没有从 phone 卸载应用程序),存储的图像文件将是失踪。这是预期的吗?当我的应用程序已在 App Store 上发布以及用户更新应用程序时,我会遇到类似的问题吗?当我更新 Xcode 中的一些代码时,当我重新 运行 它们时,有没有办法保持文件持久?
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"file.jpeg",]];
if (![imageData writeToFile:imagePath atomically:YES])
{
// Handling when there is an error
NSLog(@"Failed to store file at path %@",imagePath);
stored.text = @"";
}
else
{ // Handling when write is successful
NSLog(@"Imaged stored path is %@",imagePath);
stored.text = imagePath;
}
[imageView setImage:image];
读取图片的代码如下:
NSString *imagePathToRead = stored.text;
NSLog(@"Retrieve image from %@",imagePathToRead);
UIImage *image = [UIImage imageWithContentsOfFile:imagePathToRead];
[imageView setImage:image];
我在这里发布了 rmaddy 的答案,它解决了我面临的问题。我在这里发帖是为了让其他遇到类似问题的人受益。
对于存储部分:
stored.text 不应存储完整路径:
stored.text = imagePath;
相反,我应该只存储@"file.jpeg"。
阅读部分:
当我想读取路径而不是下面的代码时,我应该总是重新生成下面的完整路径:
NSString *imagePathToRead = stored.text;
更正后的代码是:
NSString *imagePathToRead = stored.text;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:imagePathToRead]];
NSLog(@"Retrieve image from %@",imagePath);
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
这解决了问题,即使通过 Xcode 重新安装应用程序也可以读取图像。