Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does [NSData writeToFile] write to?

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.

I went to

~Library/Application Support

and I can't find foo.png. Where is it?

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.

like image 647
user4951 Avatar asked Oct 11 '12 01:10

user4951


2 Answers

You need to direct the NSData object to the Path you want the data to be saved in:

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).

like image 132
Shachar Avatar answered Oct 05 '22 07:10

Shachar


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 :

Screenshot - show in finder executable

Then open in finder.

Here pawel.plist resource create via

NSArray *a = @[@"mybike", @"klapki", @"clapki", @"ah"];
[a writeToFile:@"pawel.plist" atomically:NO];

pawel.plist resource I done from code

like image 45
Paul Brewczynski Avatar answered Oct 05 '22 08:10

Paul Brewczynski