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
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:
- How to change background color in Android application?Best solution by Stack Overflow
- How to change the Color of each tab indicator?Best solution by Stack Overflow
- How to subtract background from an image?Best solution by Stack Overflow
- How to change the color of the white table in default layout?Best solution by Yahoo! Answers
- How to change the color of Myspace profile comments?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.