0

I try to show 256 colors in iphone or even more colors

At first ,I use photoshop to generator some simple color png file

Than I add this png to an array and set it as a button picture

This is what I done now

This is the code how I add color into array

imgArray = [NSArray arrayWithObjects:
                [UIImage imageNamed:@"Icon0.png"],
                [UIImage imageNamed:@"Icon1.png"],
                [UIImage imageNamed:@"Icon2.png"],
                [UIImage imageNamed:@"Icon3.png"],
                [UIImage imageNamed:@"Icon4.png"],
                [UIImage imageNamed:@"Icon5.png"],
                nil];

Than I add this to button image

This is the result on my simulator , but this is not a good solution

enter image description here

It waste too many time to add png file (also waste iphone memory...)

Does anyone can help me to figure this problem ???

MANY THANKS : )

Webber Lai
  • 2,014
  • 5
  • 35
  • 66

2 Answers2

6

if you just want to simply showing colors, you can subclassing UIView and override drawRect:

code is something like this:

    CGFloat red, green, blue, x, y, width, height;
    for (red = 0; red < 1; red += 1/8)
    for (blue = 0; blue < 1; blue += 1/8)
    for (green = 0; green < 1; green += 1/8) {
    //update x and y
        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];  //create the color by rgb value
        [color setFill];
        CGContextFillRect(context, CGRectMake(x, y, width, height));  //draw the rect

    }
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • +1 this is a great answer. The OP will want to read this guide by Apple on [Quartz 2D programming.](http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001066) This will help explain `drawRect:` and `CGContext` and it's methods. – Stephen Furlani Mar 03 '11 at 13:20
  • I'm not just show the colors , It need to be show on a button – Webber Lai Mar 03 '11 at 14:56
  • you may also override the touch handle method to check where does the user touch the view by using `locationInView:` in UITouch and use it to calculate what color in that pixel – Bryan Chen Mar 03 '11 at 23:31
  • a better way rather override the touch handle method is use UIGestureRecognizer – Bryan Chen Mar 03 '11 at 23:39
  • UIButton being a subclass of UIView has the property `backgroundColor`, you could create UIButtons programmatically and set their position and background colors – Chris Wagner Mar 03 '11 at 23:46
  • Thank you - you might need to replace 1/8 with 0.125 to get the proper float arithmetic happening. – Caroline Jun 07 '12 at 08:12
0

You could load only one black and white png and then tint the color of the image as shown in this question below... How would I tint an image programmatically on the iPhone?

Community
  • 1
  • 1
Skyler Saleh
  • 3,961
  • 1
  • 22
  • 35
  • Well ... I don't know this link is help me or not...your solution is put another image on a black or white color background ...But the question is how to create 000000~FFFFFF color ??? – Webber Lai Mar 03 '11 at 10:03