PDA

View Full Version : Get images a dictionary to a UIImageView




Grannyville7989
Feb 20, 2012, 09:15 AM
Hello there,

I am working on a project for my third year of computer science, in which I have to build an app for the iPad to help primary school children with their welsh.

I have a dictionary that contains PNG images, of the weather in this case, as well as a string that relates to the image.

I am trying make it so that whenever the view is loaded from the menu, the images are shown on the screen at random. I could hard code it that a specific images appear on a specific UIImageView but that would result in the images being displayed in the same place every time.

My friend has helped me with the following code but it obviously doesn't do what I want it to do.

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Weather Plist" ofType:@"plist"];
weatherDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];

RecordIndex = arc4random()%[weatherDictionary count];

image1.image = [UIImage imageNamed:[[weatherDictionary valueForKey:@"Cloudy"] valueForKey:@"image"]];

//image2
//image3
//image4

Any aid would be appreciated. I wasn't sure what else you need to know so give me a shot if you need any more information :)

EDIT: I'm aware that the title doesn't make any sense. I apologise in advance if you find it unclear/misleading in anyway.



chown33
Feb 20, 2012, 11:19 AM
Start by analyzing what your current code is trying to do.

First, you load a dictionary from a plist file. It apparently contains image-names as values, with other names like "Cloudy" as keys.

Second, you produce a random number based on the dictionary count. Or try to. So let's analyze this. First, dictionary keys and values don't have a numbered position, like an array does. So generating a random number identifying a position isn't going to be useful. In other words, even if you generate a random number between 0 and the dictionary's item count, you can't directly use that number to retrieve the Nth dictionary item.

What you can do, however, is get the keys into an NSArray, then use the random number to index into that. Look up the NSDictionary class reference, and find a method that does that.

Next, you have to shuffle the array to randomize it. You could pick random numbers and remove keys from the array, but then your view would have images, but you'd have no way of knowing which key that image belonged to. So to preserve the correlation between on-screen image views and the current randomized arrangement of keys, keep the shuffled NSArray. The Fisher-Yates shuffle (http://en.wikipedia.org/wiki/Fisher–Yates_shuffle) is a simple and effective shuffling algorithm.

At this point, you have a shuffled array of keys. To assign an image to a view, simply get a key from sequential position N of the array, use that to get the image name from the dictionary, then load and assign the image to view object N.

This solution came about by breaking things down into smaller pieces, and solving each piece. You need to understand what the piece is at each step. For example, recognizing that a random number is useless with an NSDictionary, because dictionaries don't have numbered entries, but NSArrays do.


The above assumes you have multiple image-views on-screen at once, and you want them randomly assigned to the views. It's unclear from the original post whether you mean this, or whether you mean you have one image visible at a time, and the user views them sequentially, where the sequence is randomized. Either way, the same basic approach will work: keys into array, shuffle array, take sequential keys from array.

Grannyville7989
Feb 20, 2012, 03:02 PM
Thank you. You are correct to assume that there are multiple image views on screen at once, four to be percise.

I'm very new to using programming in iOS. I've had my confidence shaken around a few times while trying to learn at university. I'll give your suggestions a crack right away :)