How to change thin image to fat image in iPhone SDK?

how to change background color of image view randomly in iphone sdk?

  • I want to change background color of image view randomly i don't want to set images by [imageView setImage:[UIImage imageNamed:@"img1.png"]]; by this code i want to change background color NSArray *colorArray = [NSArray arrayWithObjects:@"[UIColor greenColor]",@"[UIColor whiteColor]",@"[UIColor redColor]",nil]; // add all of the images to the scroll view for (int i = 1; i < 5; i++) { imageView = [[UIImageView alloc] init]; [imageView setBackgroundColor:[colorArray objectAtIndex:i-1]]; [self.scrollView addSubview:imageView]; } Can it is possible? i tried but i get error of [NSCFString CGColor]:. in my application i want to create http://itunes.apple.com/us/app/flashlight./id285281827?mt=8. can any body guide me.

  • Answer:

    You shouldn't be storing string in your array, store the actual colors objects. NSArray *colorArray = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor whiteColor], [UIColor redColor], nil]; Also, if you're no using ARC, your imageView is leaking.

Hrushikesh at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

If you need random colors out of a fixed set of 4 colors, you should store the colors as mentioned in the answer by Ecarrion. Then you could select a random color out of that. If you truly want a random color in a larger set, you could use CGFloat green=(arc4random()%100) *.01; CGFloat blue=(arc4random()%100) * .01; CGFloat red=(arc4random()%100) *.01; UIColor *imageBgColor=[UIColor colorWithRed:red green:green blue:blue alpha:1]; [imageView setBackgroundColor:imageBgColor]; Also your imageView is leaking. //fix after adding as sub view to scroll view [imageView release];

MadhavanRP

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.