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 1113287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:50:05+00:00 2026-05-17T02:50:05+00:00

I am trying to save an array of images to the documents folder. I

  • 0

I am trying to save an array of images to the documents folder. I managed to save an image as NSData and retrieve it using the method below, but saving an array seems to be beyond me. I’ve looked at several other questions that relate and it seems I’m doing everything right.

Adding the image as NSData and saving the image:

[imgsData addObject:UIImageJPEGRepresentation(img, 1.0)];
[imgsData writeToFile:dataFilePath atomically:YES];

Retrieving the data:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"imgs.dat"];
[self setDataFilePath:path];

NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:dataFilePath]) 
 imgsData = [[NSMutableArray alloc] initWithContentsOfFile:dataFilePath];

So, writing an image as NSData using the above works, but not an array of images as NSData. It inits the array, but it has 0 objects, which isn’t correct, since the array I am saving has several. Does anyone have any ideas?

  • 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-17T02:50:06+00:00Added an answer on May 17, 2026 at 2:50 am

    First of all, you should brush up Cocoa Memory Management, the first line of code is a little bit of a worry.

    For data serialisation, you may like to have a go with NSPropertyListSerialization. This class serialises arrays, dictionaries, strings, dates, numbers and data objects. It has an error reporting system, unlike the initWithContentsOfFile: methods. The method names and arguments are a bit long to fit on one line, so sometimes you may see them written with Eastern Polish Christmas Tree notation. To save your imgsData object, you can use:

    NSString *errString;
    NSData *serialized =
        [NSPropertyListSerialization dataFromPropertyList:imgsData
                                                   format:NSPropertyListBinaryFormat_v1_0
                                         errorDescription:&errString];
    
    [serialized writeToFile:dataFilePath atomically:YES];
    
    if (errString)
    {
        NSLog(@"%@" errString);
        [errString release]; // exception to the rules
    }
    

    To read it back in, use

    NSString *errString;
    NSData *serialized = [NSData dataWithContentsOfFile:dataFilePath];
    
    // we provide NULL for format because we really don't care what format it is.
    // or, if you do, provide the address of an NSPropertyListFormat type.
    
    imgsData =
        [NSPropertyListSerialization propertyListFromData:serialized
                                         mutabilityOption:NSPropertyListMutableContainers
                                                   format:NULL
                                         errorDescription:&errString];
    
    if (errString)
    {
        NSLog(@"%@" errString);
        [errString release]; // exception to the rules
    }
    

    Check the contents of errString to determine what went wrong. Keep in mind that these two methods are being deprecated in favour of the dataWithPropertyList:format:options:error: and propertyListWithData:options:format:error: methods, but these were added in Mac OS X 10.6 (I’m not sure if they’re available on iOS).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to save multiple image using this code. But instead its saving
I'm trying to save some data into an array but unfortunately all the data
I'm trying to save a CUploadedFile object in local folder, I am using img
I am trying to convert a PIL image into an array using NumPy. I
I am trying to save a file to an Image datatype using inline query.
Im trying to save a simple script in netbeans to htdocs, but i only
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save a huge NSArray to a file and then retrieve it...
I am trying to save a simple template to pdf using the rendering plugin,
I'm trying to save Images the user takes with the camera 1) If I

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.