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提供了很多你可以在应用程序 ...
随机推荐
- Android 巧妙实现图片和文字布局
之前写过一个博客是关于实现图片和文字左右或者上下布局的方法, 下面是博客的主要内容: 布局文件很简单,用来展示RadioButton的使用方法. 1 <?xml version="1. ...
- 【转】 iOS 两种方法实现左右滑动出现侧边菜单栏 slide view
原文: http://blog.csdn.net/crayondeng/article/details/9057637 --- 关于评论中,很多网友都是需要这部分的相关源码,其实在我上传的新浪微博 ...
- Google jQuery URL
Query 在线地址:https://developers.google.com/speed/libraries/devguide?hl=zh-CN#jquery此地址里还包含了很多的JS框架.
- Python学习笔记:02数据类型
Python 数据类型 python中标准的数据类型有 基础类型 整型(长整型) 浮点型 复数型 布尔型 序列类型 字符串 列表 元组 字典 整型 整型和长整型并不严格区分,整型int的表达范围和计算 ...
- Google Protocal Buffer
Google Protocal Buffer 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化或者说序列化.它很适合做数据存储或RPC数据交换格式. 串行化(序列化):将对象存储到解释中式 ...
- uva 10820 (筛法构造欧拉函数)
send a table When participating in programming contests, you sometimes face the following problem: Y ...
- 读取pdf文件 .选择了itextsharp 库
此库还是比较成熟.看博客园很多文章都介绍了此库 用法 如果项目用到读取pdf. 我这只是提供个思路.或者提供个方法.用itextsharp 能方便实现 StringBuilder text = ne ...
- zoj 3758 Singles' Day
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5203 题意:有n个1然后按照b进制转化为10进制数,判断这个数是不是素数. # ...
- poj 3308 Paratroopers
http://poj.org/problem?id=3308 #include <cstdio> #include <cstring> #include <algorit ...
- ISO14443标准详细介绍
这篇文章从各方面详细介绍了ISO/IEC14443标准.第一部分:物理特性1.范围ISO/IEC14443的这一部分规定了邻近卡(PICC)的物理特性.它应用于在耦合设备附近操作的ID-1型识别卡.I ...