Convert nested NSArray to array of images for UIScrollview based on selection made

I have an array in a plist which holds three arrays of images as follows:

<dict>
  <key>ImageArrays</key>
   <array>
      <array>
        <string>Bears.png</string>
        <string>BabyBear.png</string>
        <string>MamaBear.png</string>
      </array>
      <array>
        <string>Birds.png</string>
        <string>BabyBirds.png</string>
        <string>MamaBirds.png</string>
      </array>
      <array>
        <string>Chimps.png</string>
        <string>BabyChimp.png</string>
        <string>MamaChimp.png</string>
      </array>
    </array>

Based on a selection made within an iCarousel I want to display one of those arrays in a UIScrollView on another view controller. I'm passing the selection on via the following:

[PassInfo myImageArray]
//myImageArray is the array selected.

My question is how do I get the pictures from the array selected into another array that feeds the UIScrollview? I passed the entire array so do I now need to convert the image names to NSStrings and then put in a new array? If so, how?

EDIT: To give more info on what I've done already.

So, I've accessed the outer array from the plist like this:

-(void)awakeFromNib
  self.stopImageArray = [rootDictionary objectForKey:@"ImageArrays"];

Determined the selected array with this:

-(void)didSelectItemAtIndex    
    arrayChosen = [NSArray arrayWithObject:[self.stopImageArray objectAtIndex:self.carousel.currentItemIndex]];
    [info setMyImageArray:arrayChosen];
    [allInfo addObject:info];

Passed it via a class file with this:

@property (nonatomic, strong) NSArray *myImageArray;

And finally pulled up the selected array on the final view controller with this:

chosenArray = [NSArray arrayWithArray:[PassInfo myImageArray]];

So I think I've got the correct NSArray I need, I'm just not sure how to convert that array into an an array filled with the images. Don't I need to create an NSArray with objects or convert the image names to UIImage?

Answers


First access the internal array of the ImageArrays key. Lets say you have this:

NSArray* arrayWithArrays = // Store the first outer <array>

Then you can get any of the arrays inside with indexes:

NSArray* imageArray1 = arrayWithArrays[0];
NSArray* imageArray2 = arrayWithArrays[1];
NSArray* imageArray3 = arrayWithArrays[2];

Just make sure you do not go out of bounds on the arrayWithArrays, which you can check by calling the count method or just looping through the array like a collection:

NSArray* selectedImageArray;

for (NSArray* imageArray in arrayWithArrays)
{
    // Usually should wrap this line with a conditional
    selectedImageArray = imageArray;
}

Fill UIScrollView with selectedImageArray or any of the imageArrayX variables. Of course you can store those in a property in your View Controller class.

If you want to store the UIImage objects in an array, first you need to loop through imageArray with strings and create the objects. You can accomplish this with the following code:

NSMutableArray* images = [NSMutableArray array];

for (NSString* imageName in imageArray
{
    [images addObject:[UIImage imageNamed:imageName]];
}

However I would advise you against storing whole UIImage objects in an array. It is better to store the strings, like you do now, but just call the imageNamed: method everytime you need to load the image.


Just make sure you load the images with: [UIImage imageNamed:yourImage].


Need Your Help

Wamp PHP CLI

php cakephp wampserver

I'm running wampserver and can't seem to use php files in the CLI. Supposedly there are two modes of running php, CGI and CLI. I can't figure out how to enable CLI since I don't see php-cli.exe in...

Application safe to use READ_COMMITTED_SNAPSHOT?

sql-server-2005 isolation-level read-committed-snapshot

I have a large web application using a COM data access layer against an SQL Server 2005 database. By default, the isolation level is READ_COMMITTED. Now I understand how READ_COMMITTED_SNAPSHOT iso...