ios二維繪圖之色彩坡形

來源:互聯網
上載者:User

標籤:

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 = @[(__bridgeid)[UIColorredColor].CGColor, (__bridgeid)[UIColorblueColor].CGColor, (__bridgeid)[UIColorpurpleColor].CGColor];

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

   CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridgeCFArrayRef)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 {

    [superviewDidLoad];

    

    UIGraphicsBeginImageContext(CGSizeMake(200,200));

    

    CGContextRef context =UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [UIColoryellowColor].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 {

    

    [superdidReceiveMemoryWarning];


}


@end




ios二維繪圖之色彩坡形

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.