Skip to content

Instantly share code, notes, and snippets.

@catehstn

catehstn/ImageDrawer.m

Last active Dec 1, 2016
Embed
What would you like to do?
Creating images from an array of colors
// 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
You can’t perform that action at this time.