【问题标题】:how to fix file writing issue into documents directory?如何将文件写入问题修复到文档目录中?
【发布时间】:2016-05-21 19:55:00
【问题描述】:

我已经实现了将文件写入文档目录的逻辑。

NSData *pngData = UIImagePNGRepresentation([UIImage imageNamed:@"logo_icon_login.png"]);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];

if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath])
{
  [pngData writeToFile:filePath atomically:YES];
}
else
{
  NSLog(@"Not writable.");
}

现在问题出在某些设备上,它说不可写。在文档目录中写入文件之前我需要任何权限吗?

【问题讨论】:

标签: ios objective-c nsfilemanager nsdocumentdirectory


【解决方案1】:

创建一个名为 images 的文件夹,然后将图像放入其中。可能是根目录问题。

试试这个:

NSString *filePath = [documentsPath stringByAppendingPathComponent:@"images/image.png"];

【讨论】:

  • 如果您的数据格式正确,默认情况下文档的根目录是可写的。请参阅我上面的评论,因为这个问题似乎引用了一个已经回答的问题。
猜你喜欢
  • 1970-01-01
  • 2019-10-19
  • 1970-01-01
  • 2016-11-14
  • 2022-10-07
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
相关资源
最近更新 更多