Why is my data successfully written to a file on the iPhone simulator, but not on my device?

I have the following code that saves user sketch data to a file ...

//Auto Save the sketch
NSString *filename = [NSString stringWithFormat:@"%@.png", sketchID];
CGImageRef imageRef = CGBitmapContextCreateImage(paintView.canvas.mBitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filename atomically:YES];

CGImageRelease(imageRef);
[image release];

SketchIDis a unique alphanumeric value, so as an example of the file name I save "p1.png". The code I use to read the file ...

//Load the sketch where the user left off, if there is one
if(fileName != nil)
{
    UIImage* image = [UIImage imageWithContentsOfFile:fileName];    

    if(image != nil)
    {
          .
          .

This code seems to work fine when run on the simulator, but when I run it on the device, the image fails to load. I'm new to iOS development, and I'm still learning how files are stored. My questions...

  1. Why does saving / loading a file work on a simulator and not on a device?
  2. , , iPhone, ? "p1.png" , writeToFile: imageWithContentsOfFile: ?
+3
3

/ , ?

, , , .

iPhone "" , , .

, , iPhone, ? "p1.png" , writeToFile: ? imageWithContentsOfFile: ?

( - ) "" ( - , iTunes).

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
+5

, , . . .

: iPhone?

+2

, , , , . , filePath, . .

docsDir!+"filename.dat" // wrong does not have path separator between dir and file
docsDir!.stringByAppendingPathComponent("filename.dat") // works since it puts the "/" in between the two.
0

Source: https://habr.com/ru/post/1760776/


All Articles