ios [NSData writeToFile] 写到哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12830862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where does [NSData writeToFile] write to?
提问by user4951
I wrote a simple program to help me debug.
我写了一个简单的程序来帮助我调试。
#import "UIImage+saveScreenShotOnDisk.h"
@implementation UIImage (saveScreenShotOnDisk)
-(void)saveScreenshot{
NSData * data = UIImagePNGRepresentation(self);
[data writeToFile:@"foo.png" atomically:YES];
}
@end
After it's executed, I want to know where foo.png
is located.
执行后,我想知道foo.png
位于何处。
I went to
我去了
~Library/Application Support
and I can't find foo.png
. Where is it?
我找不到foo.png
. 它在哪里?
If I do
如果我做
BOOL result = [data writeToFile:@"foo.png" atomically:YES];
the result will be NO
, which is kind of strange given that the simulator, unlike the iPhone, can write anywhere.
结果将是NO
,这有点奇怪,因为模拟器与 iPhone 不同,可以在任何地方编写。
回答by Shachar
You need to direct the NSData object to the Path you want the data to be saved in:
您需要将 NSData 对象定向到要保存数据的路径:
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"foo.png"]];
[data writeToFile:databasePath atomically:YES];
This will save your file in your App's Folder on the device (or the simulator).
这会将您的文件保存在设备(或模拟器)上的应用程序文件夹中。
回答by Paul Brewczynski
The default place, where the files are stored, is the folder your executable is stored.
存储文件的默认位置是存储可执行文件的文件夹。
See where your executable is stored via right clicking Products->YourExecutable :
通过右键单击 Products->YourExecutable 来查看可执行文件的存储位置:
Then open in finder.
然后在finder中打开。
Here pawel.plist
resource create via
这里pawel.plist
资源创建通过
NSArray *a = @[@"mybike", @"klapki", @"clapki", @"ah"];
[a writeToFile:@"pawel.plist" atomically:NO];
回答by Gobe
For Swift, based on Shachar's answer:
对于 Swift,基于 Shachar 的回答:
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] + "/foo.png"
data?.writeToFile(path, atomically: true)
回答by Paresh Navadiya
U forgot to provide location or path where u want to write.Check this writeToFile:options:error:
你忘了提供你想写的位置或路径。检查这个writeToFile:options:error:
Writes the bytes in the receiver to the file specified by a given path.
将接收器中的字节写入给定路径指定的文件。
- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr
Parameters
参数
path
小路
The location to which to write the receiver's bytes.
mask
面具
A mask that specifies options for writing the data. Constant components are described in “NSDataWritingOptions”.
errorPtr
错误指针
If there is an error writing out the data, upon return contains an NSError object that describes the problem.
回答by neeraj
You can give a search for "NSDocumentsDirectory", I usually search the path for the documents directory using this method, and then append my file name to it. in the method writeToFile:
, this appended path is provided. Then using iTunes>application, scroll to bottom, select your app, and you should be able to see the saved file.
您可以搜索“NSDocumentsDirectory”,我通常使用此方法搜索文档目录的路径,然后将我的文件名附加到其中。在方法中writeToFile:
,提供了这个附加路径。然后使用 iTunes> 应用程序,滚动到底部,选择您的应用程序,您应该能够看到保存的文件。
PS: you should have set a valur in plist that specifies that you application uses the phone storage.
PS:您应该在 plist 中设置一个 valur 来指定您的应用程序使用手机存储。