Change RGB color of the picture
Categories: Apple, iPhone on Jan.21, 2010
Change RGB color of the picture
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | - (UIImage *) changeColor: (UIImage *)image { UIGraphicsBeginImageContext(image.size); CGRect contextRect; contextRect.origin.x = 0.0f; contextRect.origin.y = 0.0f; contextRect.size = [image size]; // Retrieve source image and begin image context CGSize itemImageSize = [image size]; CGPoint itemImagePosition; itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2); itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) ); UIGraphicsBeginImageContext(contextRect.size); CGContextRef c = UIGraphicsGetCurrentContext(); // Setup shadow // Setup transparency layer and clip to mask CGContextBeginTransparencyLayer(c, NULL); CGContextScaleCTM(c, 1.0, -1.0); CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [image CGImage]); switch (colorSelected) { case 0: CGContextSetRGBFillColor(c, 0, 0, 1, 1); break; default: CGContextSetRGBFillColor(c, 1, 0, 0., 1); break; } contextRect.size.height = -contextRect.size.height; contextRect.size.height -= 15; // Fill and end the transparency layer CGContextFillRect(c, contextRect); CGContextEndTransparencyLayer(c); UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } |
Similar posts:
November 11th, 2010 on 12:39 pm
vola nice.
Can you describe the overal strategy of the code? why flipping and masking?
May 22nd, 2011 on 3:27 pm
Thank you for your codes, it seems it works fine.
But I don’t understand why you have to do that?
contextRect.size.height -= 15;
What is the magic number “15″?
May 22nd, 2011 on 4:04 pm
@ tokentoken, i don’t remember :)
June 27th, 2011 on 4:45 pm
Hello,
I suppose 2nd line of
UIGraphicsBeginImageContext(image.size);
is not needed, and should be deleted.
Because my iPad application will crash(actually disappear) if I repeat this method, because of memory shortage.
If I delete the line, works fine.
Could you confirm?
September 9th, 2011 on 10:44 am
Maybe you could explain what this code actually does? “Change RGB color” doesn’t really explain anything.. Are you switching so the Red colors become Green? Or what exactly???
November 17th, 2011 on 2:41 pm
Nothing works for me