UIKit和Core Graphics绘图(一)——字符串,线条,矩形,渐变
概述
- 线条
- 路径
- 文本
- 图片
- 渐变
由于像素是依赖于目标的,所以2D绘图并不能操作单独的像素,我们可以从上下文(Context)读取它。
绘图就好比在画布上拿着画笔机械的进行画画,通过制定不同的参数来进行不同的绘制。
绘入字符串
NSString *fontName = @"";
NSUInteger count1 = arc4random() % ([UIFont familyNames].count);
NSString *familyName = [UIFont familyNames][count1];
NSUInteger count2 = [UIFont fontNamesForFamilyName:familyName].count;
fontName = [UIFont fontNamesForFamilyName:familyName][arc4random() % count2];
UIFont *font = [UIFont fontWithName:fontName size:30.0f];
NSString *string = @"Core Graphics";
[string drawAtPoint:CGPointMake(30.0f, 100.0f) withFont:font];
效果
绘制图片
画线
//DrawingLine
[[UIColor brownColor] set]; //设置上下文使用的颜色
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0f);
CGContextMoveToPoint(context, 30.0f, 150.0f); // 画笔移动到某点
CGContextAddLineToPoint(context, 290.0f, 150.0f);
CGContextStrokePath(context); //执行绘制
效果如下
连续绘制线条,并设置交界样式
//DrawingLinesContinuously
CGContextSetLineWidth(context, 6.0f);
CGContextSetLineJoin(context, kCGLineJoinRound); //线条交汇处样式:圆角
CGContextMoveToPoint(context, 20.0f, 150.0f);
CGContextAddLineToPoint(context, 20.0f, 80.0f);
CGContextAddLineToPoint(context, 290.0f, 80.0f);
CGContextStrokePath(context);
效果如下
绘制矩形
//DrawingRect
CGRect strokeRect = CGRectMake(25, 85, 263, 60);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0f);
CGContextStrokeRect(context, strokeRect);
绘制完成后效果
//FillRect
UIColor *clearRed = [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha:0.2];
CGContextSetFillColorWithColor(context, clearRed.CGColor);
CGContextFillRect(context, strokeRect);
效果
绘制线性渐变效果
CGColorSpaceRef space,
CFArrayRef colors,
const CGFloat locations[]
);
//DrawingGradient
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray *colors = @[(__bridge id)[UIColor colorWithRed:0.3 green:0.0 blue:0.0 alpha:0.2].CGColor,
(__bridge id)[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.8].CGColor];
const CGFloat locations[] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); CGPoint startPoint = CGPointMake(CGRectGetMinX(strokeRect), CGRectGetMinY(strokeRect)); //矩形最小x,y
CGPoint endPoint = CGPointMake(CGRectGetMaxX(strokeRect), CGRectGetMaxY(strokeRect)); //矩形最大x,y CGContextSaveGState(context);
CGContextAddRect(context, strokeRect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); //开始绘制
CGContextRestoreGState(context); //释放资源
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
效果如下
UIKit和Core Graphics绘图(一)——字符串,线条,矩形,渐变的更多相关文章
- UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图
绘制虚线 虚线绘制主要调用CGContextSetLineDash函数. 这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样 ...
- iOS绘图系统UIKit与Core Graphics
概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...
- iOS实现图形编程可以使用三种API(UIKIT、Core Graphics、OpenGL ES及GLKit)
这些api包含的绘制操作都在一个图形环境中进行绘制.一个图形环境包含绘制参数和所有的绘制需要的设备特定信息,包括屏幕图形环境.offscreen 位图环境和PDF图形环境,用来在屏幕表面.一个位图或一 ...
- Core Graphics绘图
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
- Cocoa Touch(三):图形界面UIKit、Core Animation、Core Graphics
UIKit 视图树模型 1.视图树模型 计算机图形实际上是一个视图树模型,每个视图都有一个本地坐标系.每个本地坐标系的组成部分是:原点在父坐标系中的位置,每个基在父坐标系中的位置,由此就可以根据向量的 ...
- Core Graphics框架 利用Quartz 2D绘图
首先,什么是Core Graphics和Quartz 2D? Core Graphics:是基于Quartz 2D绘图引擎的一个C语言的API绘图框架.它也是iOS开发中最基本的框架(Framewor ...
- iOS 图形处理 Core Graphics Quartz2D 教程
Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的 绘图.变换.颜色管理.脱屏 ...
- iOS图像处理之Core Graphics和OpenGL ES初见
http://www.jianshu.com/p/f66a7ca326dd iOS支持两套图形API族:Core Graphics/QuartZ 2D 和OpenGL ES.OpenGL ES是跨平台 ...
- UIKit,Core Data , Core Graphics, Core Animation,和OpenGLES框架
iOS的主要框架介绍 框架是一个目录,这个目录包含了共享库,访问共享库里代码的头文件,和其它的图片和声音的资源文件.一个共享库定义的方法或函数可以被应用程序调用. IOS提供了很多你可以在应用程序 ...
随机推荐
- MVC3中 ViewBag、ViewData和TempData的使用和区别(不是自己写的)
(网上抄的,并未消化)在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic) ...
- ASP.NET 多线程 监控任务执行情况,并显示进度条
关于多线程的基本概念和知识在本文中不多讲,而且我懂的也不是很透,说的太多误人子弟...对于我来说,做本文提到的功能够用就行,等实现其他效果不够用的时候,再深入研究 推荐看园子里的两篇博客应该就有个基本 ...
- 关闭iOS的自动更新
Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了
- js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置
<script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...
- __PUBLIC__模板文件路径配置
__PUBLIC__ 默认指向 项目文件下的Public文件夹 注意: __PUBLIC__不是一个常量,只在模板中进行替换,__ROOT__是一个常量 项目文件路径 defined('__PUBLI ...
- 使用django-mssql时候报pythoncom模块不存在
pip install django-mssql是链接sqlserver的数据库db引擎,这里用到了pythoncom模块,所以还需要安装 pip install pypiwin32 settings ...
- C++的类为什么要用指针
这个问题原来是这样的: C++的对象和C#不同,C++的对象是放在栈区的,C#的对象是引用类型. 这就意味着,C++进行类型转换的时候,由于调用了复制构造函数,其转换后,对象会丢弃一部分信息. 派生类 ...
- Skynet:特性收集
基于云风的 blog,收集 skynet 的特性以便将来在代码中一一验证. “ ... ” 部分节选自云风的 BLOG. 1. 基于 Erlang-Actor 模式的 C 实现 “把一个符合规范的 C ...
- iOS 获取手机 唯一标识-b
存贮在keychainQuery 可以统计用户使用情况 -(void)gatherMessage{ //采集用户设备信息 NSUserDefaults *userDefaults=[NSUserDef ...
- JavaScript and html的关系
HTML--------------------------->DOM, BOM, Event Request/Response------------->Ajax 日期处理 http:/ ...