Color gradient for ios 2D plotting

Source: Internet
Author: User

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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.