Wednesday, June 1, 2011

How to create a UIImage with a gradient from one or more UIColor

I recently needed to create a very specific progress bar not provided "as is" in the iOS SDK.
"The progress bar have an evolving background, a text label in foreground and an animation as the download/upload progresses. It must also be possible to tap on the progress bar like on a button.".

Solving this issue is pretty simple actually; you only need to use two overlapping buttons, one for the background and another for both the text label and the progression image. The latter has a clearColor background, at least for the "not yet filled" part of the progress image of course.

The point of this article is to provide the piece of code which allowed me to create this progress image set on the foreground button.

/* Create a Core Graphics Image context with the width and height of the progress bar button
    and push this context on the stack.  At this point, the image is clearColored. */

    UIGraphicsBeginImageContext(CGSizeMake(width, height));             
    CGContextRef context = UIGraphicsGetCurrentContext();           

/* Set the color to use for the fillRect method. Here we want a blue filling. */
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
/* Fill the Core Graphics Image with the color defined above given 'progress' the percent value as float between 0  and 1. */
    CGContextFillRect(context, CGRectMake(0, 0, width * progress, height));

/* Now we can remove the graphic context from the stack, retrieve the freshly created UIImage and of course, release the Core Graphics image context we created at first. */
        UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

/* Set the new background. */
    [myProgressButton setBackgroundImage:outputImage forState:UIControlStateNormal];

1 comment:

  1. If you proceed to have issues, Stylus Pens rub a normal glue stick on the print mattress right before printing. Recreate pretty much anything by putting it on this 3D scanner, the place a rotating base and built-in digicam create a 360-degree copy, which is then editable in any 3D program and printable on your 3D printer. Simply scan the item, import the scan into your slicing software program for cleanup, and print. The included software program alerts you of next steps within the printing process with both sound or texts. Scan quality and print resolution are nice, and setup is easy, although you would possibly need to clear up your 3D mannequin a bit in a 3D software program app after. In March 2014, surgeons in Swansea used 3D printed parts to rebuild the face of a motorcyclist who had been critically injured in a highway accident.