Last active
December 1, 2016 05:30
-
-
Save catehstn/8bd0391e04b38be99c80 to your computer and use it in GitHub Desktop.
Creating images from an array of colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make an image all of one size, in whatever color. | |
+ (UIImage *)createTestImageWithWidth:(CGFloat)width | |
height:(CGFloat)height | |
color:(UIColor *)color { | |
CGRect rect = CGRectMake(0, 0, width, height); | |
UIGraphicsBeginImageContext(rect.size); | |
[color set]; | |
UIRectFill(rect); | |
UIImage *testImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return testImage; | |
} | |
// Create a 3x3 image alternating between two colors. | |
+ (UIImage *)createTestImageNineQuadrantsWithColor1:(UIColor *)color1 | |
color2:(UIColor *)color2 { | |
// Create an image with 4 quadrants of color. | |
CGRect rect = CGRectMake(0, 0, 3.0, 3.0); | |
UIGraphicsBeginImageContext(rect.size); | |
[color1 set]; | |
UIRectFill(CGRectMake(0, 0, 1, 1)); | |
UIRectFill(CGRectMake(2, 0, 1, 1)); | |
UIRectFill(CGRectMake(1, 1, 1, 1)); | |
UIRectFill(CGRectMake(0, 2, 1, 1)); | |
UIRectFill(CGRectMake(2, 2, 1, 1)); | |
[color2 set]; | |
UIRectFill(CGRectMake(1, 0, 1, 1)); | |
UIRectFill(CGRectMake(0, 1, 1, 1)); | |
UIRectFill(CGRectMake(2, 1, 1, 1)); | |
UIRectFill(CGRectMake(1, 2, 1, 1)); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
// Create a 2x2 image with each quadrant a different color. | |
+ (UIImage *)createTestImageWithFourColors { | |
// Create an image with 4 quadrants of color. | |
CGRect rect = CGRectMake(0, 0, 2.0, 2.0); | |
UIGraphicsBeginImageContext(rect.size); | |
[[UIColor redColor] set]; | |
UIRectFill(CGRectMake(0, 0, 1, 1)); | |
[[UIColor greenColor] set]; | |
UIRectFill(CGRectMake(1, 0, 1, 1)); | |
[[UIColor blueColor] set]; | |
UIRectFill(CGRectMake(0, 1, 1, 1)); | |
[[UIColor blackColor] set]; | |
UIRectFill(CGRectMake(1, 1, 1, 1)); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment