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

  • Home
  • SEARCH
  • 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 8410429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:10:25+00:00 2026-06-10T00:10:25+00:00

Hi I want to save an image to a directory, I pass the NSData

  • 0

Hi I want to save an image to a directory, I pass the NSData and do what I think will save the file in a directory I create but the problem is that it doesn’t save. This is what I have so far. Why doesn’t the initWithContentsOfURL:encoding:error: work, it returns null but the other method I used works? The main problem is WRITETOURL which returns a 0 which i think means that the information wasn’t stored properly, any tips?

     NSFileManager *fm = [[NSFileManager alloc] init];
        NSArray * directoryPaths = [fm URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
        NSLog(@"%@", directoryPaths);
        NSURL* dirPath = nil;    
        dirPath = [[directoryPaths objectAtIndex:0] URLByAppendingPathComponent:[NSString stringWithFormat:@"photos.jpeg"]];
       NSError* theError = nil;
       [fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError];

    UIImage* photoToStore = [UIImage imageWithData:photoToSave];

    NSString *pathContainingPhoto = [[NSString alloc] initWithFormat:@"%@.jpeg", UIImageJPEGRepresentation(photoToStore, 1.0)];
    NSError *error = nil;


    BOOL OK = [pathContainingPhoto writeToURL:dirPath atomically:YES encoding:NSUTF8StringEncoding error:&error];

    NSLog(@"OK = %d", OK); //Returns 0
    NSLog(@"%@", dirPath);

    //WHY DOESNT THIS VERSION WORK?
    // NSString *pathToFile = [[NSString alloc] initWithContentsOfURL:dirPath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@", pathToFile);

      NSString* pathToFile = [NSString stringWithContentsOfURL:dirPath encoding:nil error:nil];
    NSArray *dirContents = [fm contentsOfDirectoryAtPath:pathToFile error:nil];
    NSLog(@"%@", dirContents);
  • 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-06-10T00:10:27+00:00Added an answer on June 10, 2026 at 12:10 am

    Do like this and int count in .h file and set its intial value count = 0; in viewDidLoad:

    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"Image_%d.png",count];
    error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]) // removing item it already exuts
    {
       [[NSFileManager defaultManager] removeItemAtPath:stringPath error:&error];
    }
    
    if(photoToSave) // nsdata of image that u have
    {
      [photoToSave writeToFile:stringPath atomically:YES];
    }
    count++; // maintaining count of images
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save an image that I draw, but a failure is always
I'm trying to save my file in a root directory. but I keep getting
I am using the following: $chartImage->autoOutput('/statistics/'.$image.'.png'); The problem is that this code outputs the
I want to store each individual image into it's own directory which will be
I want to build a C# application that save a pps\ppt file as SWF.
I want a user to pick a directory to save image files too. I'm
I want to save webpage in document directory with the images,css and javascripts etc..
I want to save Image in Database with JSON web service .without image data
I have an image in an UIImageView and want to save it to the
I'm using 3d in WPF and want to save it to a 2d image

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.