0

I want to add create a 2-d array in objective c for example int a[0][i] like this unspecified number of column.I have a normal NSMutableArray contains image in it.Now what I need is if I take a 3 photos for first time it has to save in column 0 then I'll take another 4 photos it has to save in column 1.Initially if I take 3 photos these images is saved first in a NSMutableArray named "image_name".Now the bunch of images has to move to index path 0 then second time I'll take for example 2 photos it has to save in index path 1 and it has to go on.

The index path has to be unspecified and I have to insert bunch of images from a NSMutableArray named "image_name" to index path 0 then clear the "image_name" array now capture bunch of images move to index path 1 and it has to move on ?

Please help how to create a dynamic 2-d array ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ramanan R R
  • 896
  • 8
  • 18
  • 1
    Check these links 1. http://stackoverflow.com/questions/7459931/objective-c-multi-dimensional-array 2. http://stackoverflow.com/questions/13034534/how-to-build-a-multidimensional-array-of-objects-in-objective-c – user2071152 Apr 10 '14 at 06:22
  • @user2071152 Mark as duplicate? – CrimsonChris Apr 10 '14 at 06:29
  • you can put each set of images in to NSArray and add that array object into your NSMutableArray named "image_name". – Keshav Apr 10 '14 at 06:29

4 Answers4

0

Instead of storing images in your NSMutableArray, store other NSMutableArrays that then contain your images.

NSArray *my2DImageArray = @[ @[ image00, image01, image02 ], @[ image10, image11, image12 ], @[ image20, image21 ], @[ image30 ], ];

CrimsonChris
  • 4,651
  • 2
  • 19
  • 30
  • But using literals won't let you to use it dynamically. – javierfdezg Apr 10 '14 at 07:03
  • @oski555 Why not? If I wanted to add a new array I could just use `arrayByAddingObject`. Whether or not the arrays are stored as mutable or immutable is an implementation detail that isn't part of the OP's question. – CrimsonChris Apr 10 '14 at 07:08
0
NSMutableArray *firstDimensionArray = [[NSMutableArray alloc] init];

NSMutableArray *oneArray = [[NSMutableArray alloc] init];
NSMutableArray *twoArray = [[NSMutableArray alloc] init];
NSMutableArray *threeArray = [[NSMutableArray alloc] init];

[firstDimensionArray addObject:oneArray];
[firstDimensionArray addObject:twoArray];
[firstDimensionArray addObject:threeArray];

//Edit: added anObject declaration to avoid xCode warnings/errors in case of copy/pasting...    
NSObject *yourObject;

// Insert an object in the 'threeArray'
//Edit 2: added cast

[(NSMutableArray *)[firstDimensionArray objectAtIndex:2] addObject:yourObject]
javierfdezg
  • 2,087
  • 1
  • 22
  • 31
  • This code will produce a warning/error. `addObject` is not a method on `id` which is the return value of `objectAtIndex`. You must cast it to an NSMutableArray. Even better, write a method that gives you the NSMutableArray for an index. – CrimsonChris Apr 10 '14 at 06:57
  • The warning/error I mentioned was not addressed in your last edit. The problem is that `addObject` is an `NSMutableArray` method but `objectAtIndex` gives you an `id` NOT an `NSMutableArray.` – CrimsonChris Apr 10 '14 at 07:17
  • 1
    Fine, the code works in my environment but to be purist, I've added a cast to the result of the objectAtIndex. Thanks! – javierfdezg Apr 10 '14 at 07:22
0

Please check below code,

For the main array you have to create one NSMutableArray which u need to initialise at the first time.

NSMutableArray *arrFinal=[[NSMutableArray alloc] init];

Now For the images which u captured, create one array lets say

NSMutableArray *arrImage_Name=[[NSMutableArray alloc] init];

Now whenever u capture the image add it into the data like this,

[arrImage_Name addObject:Image1];
[arrImage_Name addObject:Image2];
[arrImage_Name addObject:Image3];

Now add it to main array

[arrFinal addObject:arrImage_Name];

then release arrImage_Name.

[arrImage_Name release];
arrImage_Name=nil;

You have to use arrImage_Name each time when ever u capture the image and would line to save it into final image array.

whenever u capture the first image allocate arrImage_Name first then do the next processing.

arrFinal will be released in the end when you done with it or navigate to next screen I hope it will help you.

svrushal
  • 1,612
  • 14
  • 25
0

You can create a main array and add set of images in newly created array and add this local array to the main array.

iDeveloper
  • 223
  • 2
  • 5