Color gradient for ios 2D plotting
Color gradient for ios 2D plotting
Color Space cmyk Printing Industry RGB gradient color: linear gradient, ring gradient linear gradient: the gradient area is the current context, perpendicular to startPoint <-> endPoint line segment, and the linear ring gradient at the intersection of this line segment:
Corefoundation (c) -- foundation (oc) Conversion forced conversion: __bridge id (type)
/*****/Automatic reference count
Save the drawn image and create the image context -- get the canvas --...... -- Get image -- Save image -- compress the image to an end: png (lossless compression) jpg (lossy compression)
# Import "LinearGradient. h"
@ Implementation LinearGradient
// Linear gradient
-(Void) drawRect :( CGRect) rect {
CGContextRef context = UIGraphicsGetCurrentContext ();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
// Color component Representation
CGFloat components [] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0 };
// Color position
CGFloat locations [] = {1.0, 0.0, 0.5 };
CGGradientRef gradient = CGGradientCreateWithColorComponents (colorSpace, components, locations, 3 );
// The gradient area is the current context, perpendicular to the startPoint <-> endPoint line segment, and the line that the line segment Intersection
CGContextDrawLinearGradient (context, gradient, CGPointMake (100, 0), CGPointMake (200, 0), 0 );
CGFloat locations2 [] = {0.4, 1.0 };
CGGradientRef gradient2 = CGGradientCreateWithColorComponents (colorSpace, components, locations2, 2 );
// The gradient area is the current context, perpendicular to the startPoint <-> endPoint line segment, and the line that the line segment Intersection
CGContextDrawLinearGradient (context, gradient2, CGPointMake (210, 0), CGPointMake (310, 0), 0 );
}
@ End
# Import "RadialGradientView. h"
@ Implementation RadialGradientView
// Ring gradient
-(Void) drawRect :( CGRect) rect {
CGContextRef context = UIGraphicsGetCurrentContext ();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
NSArray * array = @ [(_ bridge id) [UIColor redColor]. CGColor, (_ bridge id) [UIColor blueColor]. CGColor, (_ bridge id) [UIColor purpleColor]. CGColor];
CGFloat locations [] = {0.0, 0.5, 1.0 };
CGGradientRef gradient = CGGradientCreateWithColors (colorSpace, (_ bridge CFArrayRef) array, locations );
CGContextDrawRadialGradient (context, gradient, CGPointMake (200,200), 0, CGPointMake (150,200), 100, 0 );
}
@ End
// Save the drawn Image
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
UIGraphicsBeginImageContext (CGSizeMake (200,200 ));
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSetFillColorWithColor (context, [UIColor yellowColor]. CGColor );
CGContextFillRect (context, CGRectMake (0, 0,100,100 ));
UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();
NSData * data = UIImageJPEGRepresentation (image, 1.0 );
// NSData * data = UIImagePNGRepresentation (image );
[Data writeToFile: @ "/Users/apple/desktop/image.jpg" atomically: YES];
UIGraphicsEndImageContext ();
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
@ End