码迷,mamicode.com
首页 > 移动开发 > 详细

ios二维绘图之颜色渐变

时间:2015-05-04 22:16:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

ios二维绘图之颜色渐变

颜色空间
cmyk印刷行业
RGB
渐变颜色:线性渐变,环状渐变
线性渐变:渐变的区域是当前context,垂直于startPoint <-> endPoint线段,并且于这条线段相交的直线
环状渐变:

corefoundation(c) -- foundation(oc)转换
强制转换:__bridge id(类型)

/****/自动引用计数

将绘制图片保存起来
创建image context -- 获取画布 -- …… -- 获取图片 -- 保存图片 -- 结束绘制
压缩成图片:
png(无损压缩)
jpg(有损压缩)


#import "LinearGradient.h"


@implementation LinearGradient

//线性渐变

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    //颜色的分量表示

    CGFloat components[] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0};

    //颜色的位置

    CGFloat locations[] = {1.0, 0.0, 0.5};

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 3);

    //渐变的区域是当前context,垂直于startPoint <-> endPoint线段,并且于这条线段相交的直线

    CGContextDrawLinearGradient(context, gradient, CGPointMake(100, 0), CGPointMake(200, 0), 0);

    

    CGFloat locations2[] = {0.4, 1.0};

    CGGradientRef gradient2 = CGGradientCreateWithColorComponents(colorSpace, components, locations2, 2);

    //渐变的区域是当前context,垂直于startPoint <-> endPoint线段,并且于这条线段相交的直线

    CGContextDrawLinearGradient(context, gradient2, CGPointMake(210, 0), CGPointMake(310, 0), 0);

}


@end



#import "RadialGradientView.h"


@implementation RadialGradientView

//环状渐变

- (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





//将绘制图片保存

#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




ios二维绘图之颜色渐变

标签:

原文地址:http://blog.csdn.net/summer493506332/article/details/45484901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!