RanLearns
RanLearns

Reputation: 4166

placeholder variable for a UIImageView

I know that I can do this:

switch (imageNumber) {
     case 1: image1.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
     case 2: image2.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
     case 3: image3.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
     case 4: image4.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]]; break;
}

I want to be more efficient in my code, so I am wondering if there is a way to do it like this:

switch (imageNumber) {
     case 1: //somehow set image1 as the imageView I want used
     case 2: //somehow set image2 as the imageView I want used
     case 3: //somehow set image3 as the imageView I want used
     case 4: //somehow set image4 as the imageView I want used
}
imageWhicheverWasSet.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];

Thanks in advance for your help!

Upvotes: 0

Views: 2196

Answers (4)

Filip Radelic
Filip Radelic

Reputation: 26683

The second code snippet you pasted yourself is actually very close to a solution. You just need to set UIImageView *imageToChange; as instance variable and then do:

imageToChange.image = nil; // will clear the image on last selected image view
// if you don't need that, just remove that line and leave others
switch (imageNumber) {
    case 1: imageToChange = image1; break;
    case 2: imageToChange = image2; break;
    case 3: imageToChange = image3; break;
    case 4: imageToChange = image4; break;
}
imageToChange.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];

Upvotes: 1

epatel
epatel

Reputation: 46051

Another way could be to use an array

UIImageView *iv[] = {
  image1,
  image2,
  image3,
  image4,
};
if (imageNumber>=0 && imageNumber<4) 
  iv[imageNumber].image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
                                           pathForResource:@"imageName" 
                                                    ofType:@"jpg"]]

Upvotes: 0

bendytree
bendytree

Reputation: 13627

You could put the UIImageViews in an array, then use the index to select the correct one. Not sure this would be useful unless you had a lot more UIImageViews to deal with.

NSArray* imageViews = [NSArray arrayWithObjects: image1, image2, image3, image4, nil];

UIImageView* theImage = [imageViews objectAtIndex: imageNumber-1];
UIImage* theImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];

You'd want to validate the imageNumber to make sure it was within range.

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47759

How about --

UIImage* theImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];
switch ...
    case 1:image1.image = theImage; break;
    case 2:...

You could also do --

MyImageType* tempImageObj = nil;
switch ... {
    case 1:tempImageObj = &image1; break; // Or just image1, if ".image" is a property, not a field
    case 2:...
}
tempImageObj ->image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"]];  // Or "." instead of "->" if property.

Upvotes: 0

Related Questions