summary refs log tree commit diff stats
path: root/Classes
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-08-22 19:52:08 -0400
committerStarla Insigna <starla4444@gmail.com>2011-08-22 19:52:08 -0400
commit2ac50443ddbf69b7594808ba4e6de49eecbc0b84 (patch)
tree5cdd3a92e9534dd7a2d8b5b52ca9b039587210ea /Classes
parent7e84858da5ecb1a445982860ce177c3c91318135 (diff)
downloadcartcollect-2ac50443ddbf69b7594808ba4e6de49eecbc0b84.tar.gz
cartcollect-2ac50443ddbf69b7594808ba4e6de49eecbc0b84.tar.bz2
cartcollect-2ac50443ddbf69b7594808ba4e6de49eecbc0b84.zip
Added borders to level selection images
The image for each level selection now has a tutorial bubble-esque border around it, which was achieved using image masking. The border is included in the graying-out effect when the image is being tapped, and it is also included in the grayscaling when the level is not yet unlocked.

Also rewrote most of the selection drawing code. The titles are now drawn above the image, with Quartz instead of Cocos2D. The highscore label is also drawn with Quartz, though it is still below the image. The unlock condition label no longer appears, and instead, locked levels are now tappable and tapping them shows an alert view containing the unlock condition. Will probably be changed to be more elegant later.

Really, the biggest thing left before the selection screen is done is the ability to scroll through levels. The background also needs changing, but that will come later once I recruit a graphics designer. :P

Refs #207
Diffstat (limited to 'Classes')
-rw-r--r--Classes/GameModeSelection.h8
-rw-r--r--Classes/GameModeSelection.m195
-rw-r--r--Classes/GameModeSelectionLayer.m15
-rw-r--r--Classes/UIImage+ColorMasking.h15
-rw-r--r--Classes/UIImage+ColorMasking.m77
5 files changed, 247 insertions, 63 deletions
diff --git a/Classes/GameModeSelection.h b/Classes/GameModeSelection.h index 66d0a5a..f6aaeff 100644 --- a/Classes/GameModeSelection.h +++ b/Classes/GameModeSelection.h
@@ -12,19 +12,15 @@
12 NSString* name; 12 NSString* name;
13 NSString* location; 13 NSString* location;
14 BOOL unlocked; 14 BOOL unlocked;
15 int highscore;
16 NSString* unlockCondition; 15 NSString* unlockCondition;
17
18 CCLabelBMFont* nameLabel;
19 CCLabelBMFont* otherLabel;
20} 16}
21 17
22@property (readonly) NSString* name; 18@property (readonly) NSString* name;
23@property (readonly) NSString* location; 19@property (readonly) NSString* location;
24@property (readonly) BOOL unlocked; 20@property (readonly) BOOL unlocked;
25@property (nonatomic,assign) int highscore;
26@property (nonatomic,retain) NSString* unlockCondition;
27- (id)initWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked; 21- (id)initWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked;
22- (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore;
23- (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition;
28- (void)buttonTapped; 24- (void)buttonTapped;
29 25
30@end 26@end
diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m index 0816828..fc6797a 100644 --- a/Classes/GameModeSelection.m +++ b/Classes/GameModeSelection.m
@@ -9,10 +9,11 @@
9#import "GameModeSelection.h" 9#import "GameModeSelection.h"
10#import "TutorialMode.h" 10#import "TutorialMode.h"
11#import "ClassicGameMode.h" 11#import "ClassicGameMode.h"
12#import "UIImage+ColorMasking.h"
12 13
13@implementation GameModeSelection 14@implementation GameModeSelection
14 15
15@synthesize name, location, unlocked, highscore, unlockCondition; 16@synthesize name, location, unlocked;
16 17
17- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked; 18- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked;
18{ 19{
@@ -26,92 +27,186 @@
26 location = m_location; 27 location = m_location;
27 unlocked = m_unlocked; 28 unlocked = m_unlocked;
28 29
29 if (!unlocked) 30 NSString* filenameMod;
31
32 if (unlocked)
30 { 33 {
34 filenameMod = [NSString stringWithFormat:@"%@-unlocked", filename];
35 } else {
36 filenameMod = [NSString stringWithFormat:@"%@-locked", filename];
31 name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0]; 37 name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0];
32 location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0]; 38 location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0];
33 } 39 }
34 40
35 nameLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%@ (%@)", location, name] fntFile:@"levelnames.fnt"]; 41 // First, create the frame that we will put the level picture inside
42 CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage];
43 CGImageRef topLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 8, 8));
44 CGImageRef topRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 0, 8, 8));
45 CGImageRef bottomLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 8, 8, 8));
46 CGImageRef bottomRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 8, 8, 8));
47 CGImageRef topBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 16, 8, 8));
48 CGImageRef leftBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 16, 8, 8));
49 CGImageRef rightBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 24, 8, 8));
50 CGImageRef bottomBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 24, 8, 8));
51 UIImage* topLeft = [UIImage imageWithCGImage:topLeftRef];
52 UIImage* topRight = [UIImage imageWithCGImage:topRightRef];
53 UIImage* bottomLeft = [UIImage imageWithCGImage:bottomLeftRef];
54 UIImage* bottomRight = [UIImage imageWithCGImage:bottomRightRef];
55 UIImage* topBorder = [UIImage imageWithCGImage:topBorderRef];
56 UIImage* leftBorder = [UIImage imageWithCGImage:leftBorderRef];
57 UIImage* rightBorder = [UIImage imageWithCGImage:rightBorderRef];
58 UIImage* bottomBorder = [UIImage imageWithCGImage:bottomBorderRef];
59 CGImageRelease(topLeftRef);
60 CGImageRelease(topRightRef);
61 CGImageRelease(bottomLeftRef);
62 CGImageRelease(bottomRightRef);
63 CGImageRelease(topBorderRef);
64 CGImageRelease(leftBorderRef);
65 CGImageRelease(rightBorderRef);
66 CGImageRelease(bottomBorderRef);
36 67
37 [self addChild:nameLabel]; 68 CGSize boxSize = CGSizeMake(128+12, 128+12);
38 nameLabel.position = ccp(0, -32-nameLabel.contentSize.height); 69 UIGraphicsBeginImageContext(boxSize);
70 CGContextRef context = UIGraphicsGetCurrentContext();
71 [topLeft drawInRect:CGRectMake(0, 0, 8, 8)];
72 [topBorder drawInRect:CGRectMake(8, 0, boxSize.width-16, 8)];
73 [topRight drawInRect:CGRectMake(8+boxSize.width-16, 0, 8, 8)];
74 [rightBorder drawInRect:CGRectMake(8+boxSize.width-16, 8, 8, boxSize.height-16)];
75 [bottomRight drawInRect:CGRectMake(8+boxSize.width-16, 8+boxSize.height-16, 8, 8)];
76 [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height-16, boxSize.width-16, 8)];
77 [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height-16, 8, 8)];
78 [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height-16)];
79 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
80 CGContextFillRect(context, CGRectMake(8, 8, boxSize.width-16, boxSize.height-16));
81 UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
39 82
83 // Now we want to put the level image inside the frame without messing up the frame itself
40 UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]]; 84 UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]];
41 UIGraphicsBeginImageContext(CGSizeMake(128, 128)); 85 CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]);
42 CGContextRef context = UIGraphicsGetCurrentContext(); 86 [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)];
43 UIGraphicsPushContext(context); 87 selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
88 UIGraphicsEndImageContext();
89
90 // The frame needs a shadow, so let's redraw it in a new context
91 UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+10, boxSize.height+10));
92 context = UIGraphicsGetCurrentContext();
93 CGContextSaveGState(context);
44 94
45 if (unlocked) 95 if (unlocked)
46 { 96 {
47 [innerPicture drawInRect:CGRectMake(0, 0, 128, 128)]; 97 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
98 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)];
48 } else { 99 } else {
100 // Draw the picture in grayscale if the level has not yet been unlocked
101 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
49 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); 102 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
50 CGContextFillRect(context, CGRectMake(0, 0, 128, 128)); 103 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
51 [innerPicture drawInRect:CGRectMake(0, 0, 128, 128) blendMode:kCGBlendModeLuminosity alpha:1.0]; 104 CGContextRestoreGState(context);
105
106 CGContextSaveGState(context);
107 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
108 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height) blendMode:kCGBlendModeLuminosity alpha:1.0];
52 } 109 }
53 110
54 UIGraphicsPopContext(); 111 CGContextRestoreGState(context);
55 CGImageRef innerPictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; 112 CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
56 113
57 if (unlocked) 114 CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", filenameMod]];
58 {
59 CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@", filename]];
60 UIGraphicsPushContext(context);
61 CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
62 CGContextFillRect(context, CGRectMake(0, 0, 128, 128));
63 UIGraphicsPopContext();
64 CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
65 CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filename]];
66
67 CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
68 CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil];
69 theMenu.position = ccp(0, 32);
70 [self addChild:theMenu];
71 } else {
72 CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@-locked", filename]];
73 picture.position = ccp(0, 32);
74 [self addChild:picture];
75 }
76 115
116 // We're also going to need a "selected" image state for the button
117 CGContextSaveGState(context);
118 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
119 CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
120 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
121 CGContextRestoreGState(context);
122 CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
77 UIGraphicsEndImageContext(); 123 UIGraphicsEndImageContext();
124 CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]];
125
126 CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
127 CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil];
128 theMenu.position = ccp(-5, 0);
129 [self addChild:theMenu];
130
131 // Render the titles
132 UIFont* titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.0f];
133 CGSize titleSize = [location sizeWithFont:titleFont constrainedToSize:CGSizeMake(128, 0)];
134 UIFont* subtitleFont = [UIFont fontWithName:@"AmericanTypewriter" size:18.0f];
135 CGSize subtitleSize = [name sizeWithFont:subtitleFont constrainedToSize:CGSizeMake(128, 0)];
136 CGSize combinedTitleSize = CGSizeMake(128, titleSize.height + 10 + subtitleSize.height + 10);
137
138 UIGraphicsBeginImageContext(combinedTitleSize);
139 context = UIGraphicsGetCurrentContext();
140 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
141 [location drawInRect:CGRectMake(10, 0, 128, titleSize.height) withFont:titleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
142 [name drawInRect:CGRectMake(10, titleSize.height, 128, subtitleSize.height) withFont:subtitleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
143
144 CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
145 UIGraphicsEndImageContext();
146 CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]];
147 titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2);
148 [self addChild:titleSprite];
78 } 149 }
79 150
80 return self; 151 return self;
81} 152}
82 153
83- (void)setHighscore:(int)m_highscore 154- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename highscore:(int)m_highscore
84{ 155{
85 if (unlocked) 156 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:YES];
157
158 if (nil != self)
86 { 159 {
87 highscore = m_highscore; 160 if (m_highscore != 0)
88 161 {
89 otherLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"Highscore: %d", highscore] fntFile:@"leveldescriptions.fnt"]; 162 // Render the highscore label
90 otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height); 163 NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", m_highscore];
91 [self addChild:otherLabel]; 164 UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
165 CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
166
167 UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
168 CGContextRef context = UIGraphicsGetCurrentContext();
169 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
170 [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
171
172 CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
173 UIGraphicsEndImageContext();
174 CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore", m_filename]];
175 highscoreSprite.position = ccp((128-highscoreSize.width)/2, 0-64-(highscoreSize.height)/2-10);
176 [self addChild:highscoreSprite];
177 }
92 } 178 }
179
180 return self;
93} 181}
94 182
95- (void)setUnlockCondition:(NSString *)m_unlockCondition 183- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename unlockCondition:(NSString*)m_unlockCondition
96{ 184{
97 if (!unlocked) 185 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:NO];
186
187 if (nil != self)
98 { 188 {
99 unlockCondition = m_unlockCondition; 189 unlockCondition = m_unlockCondition;
100
101 otherLabel = [CCLabelBMFont labelWithString:unlockCondition fntFile:@"leveldescriptions.fnt"];
102 otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height);
103 [self addChild:otherLabel];
104 } 190 }
191
192 return self;
105} 193}
106 194
107- (void)buttonTapped 195- (void)buttonTapped
108{ 196{
109 if ([name isEqual:@"Tutorial"]) 197 if (unlocked)
110 {
111 [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
112 } else if ([name isEqual:@"Collect"])
113 { 198 {
114 [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]]; 199 if ([name isEqual:@"Tutorial"])
200 {
201 [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
202 } else if ([name isEqual:@"Collect"])
203 {
204 [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]];
205 }
206 } else {
207 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
208 [alertView show];
209 [alertView release];
115 } 210 }
116} 211}
117 212
diff --git a/Classes/GameModeSelectionLayer.m b/Classes/GameModeSelectionLayer.m index 3e1995e..a90606e 100644 --- a/Classes/GameModeSelectionLayer.m +++ b/Classes/GameModeSelectionLayer.m
@@ -42,27 +42,28 @@
42 [self addChild:tutorialSelection]; 42 [self addChild:tutorialSelection];
43 43
44 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 44 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
45 GameModeSelection* collectSelection = [[[GameModeSelection alloc] initWithName:@"Collect" location:@"Paris" filename:@"paris" unlocked:[defaults boolForKey:@"hasDoneTutorial"]] autorelease]; 45 GameModeSelection* collectSelection;
46 collectSelection.position = ccp(400-32, 160);
47 46
48 if (collectSelection.unlocked) 47 if ([defaults boolForKey:@"hasDoneTutorial"])
49 { 48 {
50 const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 1"; 49 const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 1";
51 sqlite3_stmt* compiled_statement; 50 sqlite3_stmt* compiled_statement;
51 int score = 0;
52 52
53 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) 53 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
54 { 54 {
55 if (sqlite3_step(compiled_statement) == SQLITE_ROW) 55 if (sqlite3_step(compiled_statement) == SQLITE_ROW)
56 { 56 {
57 int score = sqlite3_column_int(compiled_statement, 2); 57 score = sqlite3_column_int(compiled_statement, 2);
58
59 [collectSelection setHighscore:score];
60 } 58 }
61 } 59 }
60
61 collectSelection = [[[GameModeSelection alloc] initWithName:@"Collect" location:@"Paris" filename:@"paris" highscore:score] autorelease];
62 } else { 62 } else {
63 [collectSelection setUnlockCondition:@"Beat the tutorial!"]; 63 collectSelection = [[[GameModeSelection alloc] initWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"] autorelease];
64 } 64 }
65 65
66 collectSelection.position = ccp(320+32,160);
66 [self addChild:collectSelection]; 67 [self addChild:collectSelection];
67 68
68 CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)]; 69 CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
diff --git a/Classes/UIImage+ColorMasking.h b/Classes/UIImage+ColorMasking.h new file mode 100644 index 0000000..d25fafb --- /dev/null +++ b/Classes/UIImage+ColorMasking.h
@@ -0,0 +1,15 @@
1//
2// UIImage+ColorMasking.h
3// Cartographic
4//
5// Created by Starla Insigna on 8/22/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import <UIKit/UIKit.h>
10
11@interface UIImage (ColorMasking)
12
13- (UIImage *)opaqueMaskFromWhiteImage;
14
15@end
diff --git a/Classes/UIImage+ColorMasking.m b/Classes/UIImage+ColorMasking.m new file mode 100644 index 0000000..e4da8c8 --- /dev/null +++ b/Classes/UIImage+ColorMasking.m
@@ -0,0 +1,77 @@
1//
2// UIImage+ColorMasking.m
3// Cartographic
4//
5// Created by Starla Insigna on 8/22/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import "UIImage+ColorMasking.h"
10
11@implementation UIImage (ColorMasking)
12
13typedef enum {
14 ALPHA = 0,
15 BLUE = 1,
16 GREEN = 2,
17 RED = 3
18} PIXELS;
19
20- (UIImage *)opaqueMaskFromWhiteImage
21{
22 CGSize size = [self size];
23 int width = size.width;
24 int height = size.height;
25
26 // the pixels will be painted to this array
27 uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
28
29 // clear the pixels so any transparency is preserved
30 memset(pixels, 0, width * height * sizeof(uint32_t));
31
32 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
33
34 // create a context with RGBA pixels
35 CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
36 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
37
38 // paint the bitmap to our context which will fill in the pixels array
39 CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);
40
41 for(int y = 0; y < height; y++) {
42 for(int x = 0; x < width; x++) {
43 uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
44
45 if ((rgbaPixel[RED] == 255) && (rgbaPixel[GREEN] == 255) && (rgbaPixel[BLUE] == 255) && (rgbaPixel[ALPHA] == 255))
46 {
47 rgbaPixel[RED] = 255;
48 rgbaPixel[GREEN] = 255;
49 rgbaPixel[BLUE] = 255;
50 rgbaPixel[ALPHA] = 255;
51 } else {
52 rgbaPixel[RED] = 0;
53 rgbaPixel[GREEN] = 0;
54 rgbaPixel[BLUE] = 0;
55 rgbaPixel[ALPHA] = 0;
56 }
57 }
58 }
59
60 // create a new CGImageRef from our context with the modified pixels
61 CGImageRef image = CGBitmapContextCreateImage(context);
62
63 // we're done with the context, color space, and pixels
64 CGContextRelease(context);
65 CGColorSpaceRelease(colorSpace);
66 free(pixels);
67
68 // make a new UIImage to return
69 UIImage *resultUIImage = [UIImage imageWithCGImage:image];
70
71 // we're done with image now too
72 CGImageRelease(image);
73
74 return resultUIImage;
75}
76
77@end