Rushil
Rushil

Reputation: 338

Variable UIImageView

I have a working UIImageView, but I want to change it based on a certain variable so I have this: a string called imagevariable(which holds the value of image1 which is a png resource in my project). So I have this:

imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"imagevariable" ofType:@"png"]];

But it doesn't work. Can someone help?

Upvotes: 0

Views: 265

Answers (2)

acqu13sce
acqu13sce

Reputation: 3809

By wrapping the variable name imagevariable in quotes it is being treated as a string literal, try this instead

imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagevariable ofType:@"png"]];

Upvotes: 1

thyrgle
thyrgle

Reputation:

You are passing image variable as a string which means, that you will literally search for an image named "imagevariable.png". Instead take away that @"" and make it like this:

imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagevariable ofType:@"png"]];

Upvotes: 1

Related Questions