Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1031765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:58:53+00:00 2026-05-16T13:58:53+00:00

I have the following code that saves the users sketch data to a file…

  • 0

I have the following code that saves the users 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];

the SketchID is a unique alphanumeric value, so an example of the filename I’m saving to is “p1.png”. The code I use to read the file is…

//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 running on the simulator, but when I run it on the device, the fails to load the image. I new to iOS development and I’m still learning how files are stored. My questions are…

  1. Why would saving/loading the file work on the simulator, but not the device?
  2. When I create the filename that I want to save the data to, do I need to also include a path to a specific directory on the iPhone where the data should be properly stored? Or should “p1.png” work fine when I call the writeToFile: method? And what about the imageWithContentsOfFile: method?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T13:58:54+00:00Added an answer on May 16, 2026 at 1:58 pm

    Why would saving/loading the file work
    on the simulator, but not the device?

    There are tons of reasons, but the most common is trying to write to a location that isn’t writable on the device, and the most common reason for that is writing to the application bundle itself.

    All iPhone apps are “sandboxed” on the device, meaning they cannot access the filesystem outside of the directory the application is installed into.

    When I create the filename that I want
    to save the data to, do I need to also
    include a path to a specific directory
    on the iPhone where the data should be
    properly stored? Or should “p1.png”
    work fine when I call the writeToFile:
    method? And what about the
    imageWithContentsOfFile: method?

    You can write to your application’s temp directory (if you just need a temporarily place to put something during a run) or the Documents directory (if you need to store something more permanently, and also have it backed up by iTunes).

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that saves an object to a file... -(int) saveObject:
I have the following code that fails when i run it. my .h file:
I have the following code that returns a .CSV file when a link is
I have the following code. QString fileName = QFileDialog::getSaveFileName( this, tr(Output Image file), (),
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that gets the MAC address of an interface: static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.