Gold Challenge - Gradient on the background


#1

When I first completed this challenge I put the gradient on top of my logo. This worked, except I didn’t like the blue tinge on the propeller blades caused by the gradient. I decided that for some extra practice I wanted to see if I could put the gradient in the background behind the logo. Of course if we put the logo image as it is on top of the gradient it simply hides the gradient. We first need a way to remove the white pixels from the logo. Once we do that we can draw a white background, draw the blue to white gradient on top of the background, and then draw the modified logo on top of the gradient. I found the following method in a stackoverflow forum that makes a range of colors in a UIImage transparent (http://stackoverflow.com/questions/633722/how-to-make-one-color-transparent-on-a-uiimage):

[code]-(UIImage *)changeWhiteColorTransparent: (UIImage *)image
{
//convert to uncompressed jpg to remove any alpha channels
//this is a necessary first step when processing images that already have transparency
image = [UIImage imageWithData:UIImageJPEGRepresentation(image, 1.0)]
CGImageRef rawImageRef=image.CGImage;
//RGB color range to mask (make transparent) R-Low, R-High, G-Low, G-High, B-Low, B-High
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};

UIGraphicsBeginImageContext(image.size);
CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);

//iPhone translation
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0); 

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(maskedImageRef);
UIGraphicsEndImageContext();    
return result;

}[/code]

I implemented this method in the UIView subclass that I created for the gold solution. Then when it was time to draw the logo I did something like this:

// Get the BNR logo UIImage *img = [UIImage imageNamed:@"icon@2x.png"]; // Make white pixels transparent UIImage *imgB = [self changeWhiteColorTransparent:img]; // Draw this image to the graphics context [imgB drawInRect:rect];

Worked like a charm for me.