0

I have to create a number of UIImageViews based on values stored in an NSString. The value that is stored will become the name for my UIImageView, each one will be created individually of course.

So to keep things simple my question is, can one create a UIImageView based on the value stored within an NSString?

Rough Example:

-(UIImageView)createMyFruitViews:(NSString *)fruit
{
    NSString *fruit = @"";
    UIImageView *fruitImage [fruit value]; //<-- made up for example purposes.
    return fruitImage;
}

If this is not possible, could you please suggest a way around this?

FYI I've read through the forums too, the only thing that comes close is How to use NSSstring to instantiate a UIImageview; however I don't understand the answer and I am not sure that it can apply to what I need.

2 Answers 2

2

Try using UIImageView *fruitImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:fruit]];

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not quite sure I understood what you need... as far as I can tell, you have the name of an image on a NSString object, right?

Lets say you have an array of strings, each string representing the name of an image:

NSArray *myImageNames = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];

Then you'll create your imageViews using these images:

for (NSString *imageName in myImageNames) {
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [self.view addSubview:myImageView];
    [myImageView release]; // If you're not using ARC
}

If I didn't get exactly what you need, please let me know.

1 Comment

This may be exactly what I need, let me give it a quick try and thanks for the quick answer. By the way you understood what I need perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.