你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios二維繪圖之顏色漸變

ios二維繪圖之顏色漸變

編輯:IOS開發綜合
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




  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved