Quartz 2D在ios中的使用简述二:创建画布
在iOS中使用Quartz画图时,第一步就是要获取画布(图形上下文),然后再画布上做各种操作。先看下CoreGraphics.h这个头文件,就可以知道能够创建多少种上下文类型。
#include <CoreGraphics/CGBase.h> #include <CoreGraphics/CGAffineTransform.h>
#include <CoreGraphics/CGBitmapContext.h>
#include <CoreGraphics/CGColor.h>
#include <CoreGraphics/CGColorSpace.h>
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGDataConsumer.h>
#include <CoreGraphics/CGDataProvider.h>
#include <CoreGraphics/CGError.h>
#include <CoreGraphics/CGFont.h>
#include <CoreGraphics/CGFunction.h>
#include <CoreGraphics/CGGeometry.h>
#include <CoreGraphics/CGGradient.h>
#include <CoreGraphics/CGImage.h>
#include <CoreGraphics/CGLayer.h>
#include <CoreGraphics/CGPDFArray.h>
#include <CoreGraphics/CGPDFContentStream.h>
#include <CoreGraphics/CGPDFContext.h>
#include <CoreGraphics/CGPDFDictionary.h>
#include <CoreGraphics/CGPDFDocument.h>
#include <CoreGraphics/CGPDFObject.h>
#include <CoreGraphics/CGPDFOperatorTable.h>
#include <CoreGraphics/CGPDFPage.h>
#include <CoreGraphics/CGPDFScanner.h>
#include <CoreGraphics/CGPDFStream.h>
#include <CoreGraphics/CGPDFString.h>
#include <CoreGraphics/CGPath.h>
#include <CoreGraphics/CGPattern.h>
#include <CoreGraphics/CGShading.h>
可以看到,可以创建的上下文包括:
//图片
CGContextRef CGBitmapContextCreate
//layer(UIView)
CGContextRef CGLayerGetContext
//pdf类型
CGContextRef CGPDFContextCreate
我们再看下UIGraphics.h这个头文件,里面已经提供这3种上下文创建的封装给UIKit使用的方法,在普通的情况下,使用UIGraphics中的方法即可,而且画布已经做了坐标的翻转。

UIKIT_EXTERN CGContextRef UIGraphicsGetCurrentContext(void) CF_RETURNS_NOT_RETAINED;
表示是个弱引用类型,直接使用,不用释放。
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, , , , );
CGContextSetRGBStrokeColor(context, , , , );
CGContextFillRect(context, CGRectMake(, , , ));
NSString *text=@"文字";
UIFont *font=[UIFont systemFontOfSize:];
[text drawAtPoint:CGPointMake(, ) withAttributes:font.fontDescriptor.fontAttributes];
UIImage *img=[UIImage imageNamed:@"gg.jpg"];
[img drawInRect:CGRectMake(, , , )];
CGImageRef cgimg = CGBitmapContextCreateImage(context);
UIImage *resultImg = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
Quartz 2D在ios中的使用简述二:创建画布的更多相关文章
- Quartz 2D在ios中的使用简述一:坐标体系
Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境,官方文档:Quartz 2D Programming Guide. 一.坐标体系 这样的坐标体系就导致我们使用Quart ...
- 在iOS中使用ZBar扫描二维码
最近在做的项目中需要用到二维码扫描功能,之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Andro ...
- 使用.NET中的XML注释(二) -- 创建帮助文档入门篇
一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. 二.背景 要生成帮助文 ...
- Quartz 2D编程指南- PDF文档的创建、显示及转换
PDF文档存储依赖于分辨率的向量图形.文本和位图,并用于程序的一系列指令中.一个PDF文档可以包含多页的图形和文本.PDF可用于创建跨平台.只读的文档,也可用于绘制依赖于分辨率的图形. ...
- iOS中多线程知识总结(二)
1.GCD GCD全称是Grand Central Dispatch,译为"强大的中枢管理器" 1)什么是任务?什么是队列? 任务和队列是GCD的核心. 任务: 执行什么操作 队列 ...
- iOS中Block介绍(二)内存管理与其他特性
我们在前一章介绍了block的用法,而正确使用block必须要求正确理解block的内存管理问题.这一章,我们只陈述结果而不追寻原因,我们将在下一章深入其原因. 一.block放在哪里 我们针对不同情 ...
- iOS中的图像处理(二)——卷积运算
关于图像处理中的卷积运算,这里有两份简明扼要的介绍:文一,文二. 其中,可能的一种卷积运算代码如下: - (UIImage*)applyConvolution:(NSArray*)kernel { C ...
- iOS中 基于LBXScan库二维码扫描 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...
- ios中字典转模型的创建以及简单用法
// appModel.h // Created by zzqqrr on 17/8/19. // #import <Foundation/Foundation.h> @interface ...
随机推荐
- iOS图表库Charts集成与使用
Charts是一个很优秀的图表库,它支持Android.iOS.tvOS和macOS,这样使用起来,可以节省学习成本,可以从GitHub上了解更多信息.本文记录在iOS项目上的集成与使用. Chart ...
- ThreadStart 与ParameterizedThreadStart的区别
1) ParameterizedThreadStart与ThreadStart 1 static void Main(string[] args) { #region ParameterizedThr ...
- linux 下UGet闪退问题
安装UGet,开始使用正常,后来打开时会闪退,估计是软件配置错误,但软件重装也没用,用dpkg --purge也无法删除配置文件. 后来想到是在下载eclipse时,将eclipse文件删除,导致软件 ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- CSS实现元素水平垂直居中—喜欢对称美,这病没得治
[TOC] 在CSS中对元素进行水平居中是非常简单的:如果它是一个行内元素,就对它的父元素应用text-align:center;如果它是一个块级元素,就对它自身应用margin:auto.然而要对一 ...
- android shape的使用(转)
shape用于设定形状,可以在selector,layout等里面使用,有6个子标签,各属性如下: <?xml version="1.0" encoding="ut ...
- 限制input只能输入金额(类似:100.00|100.9|100)
$(".inputmoney").keyup(function () { var reg = $(this).val().match(/\d+\.?\d{0,2}/); ...
- SilverLight CheckBox 控件 DataContext属性与DataContextChanged事件
当CheckBox对象创建时,会触发一次DataContextChanged事件,默认值待定,销毁时不会触发,代码修改DataContext时也会触发
- 国外程序员整理的Java资源大全分享
Java 几乎是许多程序员们的入门语言,并且也是世界上非常流行的编程语言.国外程序员 Andreas Kull 在其 Github 上整理了非常优秀的 Java 开发资源,推荐给大家. 译文由 Imp ...
- org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launch IE
1.在启动ie浏览器前先加入属性设置一项: DesiredCapabilities ie = DesiredCapabilities.internetExplorer(); ie.setCapabil ...