`

Quartz 2D (ProgrammingWithQuartz) note

    博客分类:
  • ios
 
阅读更多

http://www.evernote.com/shard/s20/sh/c54af718-b04e-4645-b482-9fd1012160ef/809d946e19b8fd1f0d78b82e7157cf88

 

书链接: http://book.douban.com/subject/2003121/

 

 

Context 上下文  画板   可以是window  printer  bitmap  显示屏

 

Filling 填充
alpha (opacity)  不透明度 1为不透明  0透明  此属性决定能否看到下面的画面
opaque 不透明
stroking  画边框  在rectangle的边框(此边框无限thin)2侧画线  线有宽度

CGContextRef context = UIGraphicsGetCurrentContext();   // drawRect 方法中拿到context
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)   // 设置颜色
CGContextFillRect(context, ourRect);   //填充

CGContextSetRGBStrokeColor(context, 0.482, 0.62, 0.871, 1.0);
CGContextStrokeRectWithWidth(context, ourRect, 3.0);   // 画边框 设置边框宽度3.0

边框是画在2侧的 所以画完框在填充 会把内侧的边框覆盖掉


CGContextTranslateCTM
CGContextScaleCTM
CGContextRotateCTM

arbitrary  任意的
path   任何图像都是由path构成的a sequence of lines and curves that make up a shape, and then performing painting operations on that path

CGContextBeginPath  用空白路径替换context中已存在路径  replaces any existing path in the context with an empty path

CGContextMoveToPoint   path的起点   establishes the first point on the path and makes that the current point

CGContextAddLineToPoint  画线   add a line segment from the existing current point to the point passed to CGContextAdd- LineToPoint 

CGContextClosePath    在当前点和起点之间画一条线 完成一个shape.   This function adds a straight line segment from the current point to the initial point on the path

CGContextDrawPath    path不会产生任何图像  draw才会,同时这个方法也会清除path。 actually paint the stroked, filled, and stroked-and-filled paths, also clears the path from the context

kCGPathFill   kCGPathStroke   kCGPathFillStroke   //This type of fill-then-stroke operation is so common that Quartz defines the special kCGPathFillStroke painting mode used here

CGContextTranslateCTM(context, 200.00.0);   坐标偏移x轴正向200, 之后的draw都依据改变后的坐标  Drawing performed after calling CGContextTranslateCTM takes place in the transformed coordinate system.

CGContextSetLineWidth      sets the width of the stroke 

CGContextSaveGState(context);   保持context当前状态

CGContextRestoreGState(context); 回复context状态

CGContextRotateCTM(context, rotateAngle);   旋转坐标系 顺时针 单位是radians  一圈是2*M_PI

// 画点线
CGContextSetLineDash(context, 0., lengths, 4); //第2个参数 指定起点坐标是多少,假如是18(lengths[0]+lengths[1]),那么第一个点实际是5(lengths[2])了,  第4个参数指的用几个lengths(必须小于length总长度)
float lengths[6] = { 12.06.05.06.05.06.0 };

Clipping  裁剪
Setting up a clipping area requires creating a path to clip to, then calling the function CGContextClip to tell Quartz to constrain drawing to the area inside that path.  裁剪时先指定一个path, 然后执行裁剪方法
CGContextBeginPath(context);
CGContextAddRect(context, ourRect); 
CGContextClip(context)

CGContextClipToRect    //clipping to a rectangle specified by a CGRect

CGContextAddArc(context, circleCenter.x, circleCenter.y, circleRadius, startingAngle, endingAngle, 0);  画圆, 参数:圆心,半径, 起始角度,终点角度, 最后一个应该是否顺时针

CGContextScaleCTM(context, 1, -1);   放缩坐标   -1是倒转坐标Y轴


Window context
Bitmap graphics context
PDF graphics context
PostScript context
GLContext context
You can perform the same drawing without regard to the type of context that you are drawing to. You do the drawing and Quartz takes care of converting that drawing into the best representation for the device, or context, into which you are drawing. Device independence is one of the most powerful features of Quartz.


Using the NSImage class to draw images can result in the creation of multiple CGImage objects when drawing a given image. This produces larger PDF doc- uments since drawing the same NSImage to a given PDF context multiple times does not produce a single copy of the image data. In addition, JPEG image data is not treated specially by NSImage; therefore, during PDF genera- tion, uncompressed data is written to the PDF document

User Space and Device Space
For a PDF context, the size of a default user space unit is 1/72 of an inch, a unit of measure called a point. For a printing context, 1 user space unit is a point, regardless of the resolution of the output device used for printing. This means that for a printing context corresponding to a 300-dpi raster printer, 1 user space unit is 1/72 of an inch, so 72 user space units equals 1 inch or 300 device pixels.

The term point has its origins in the printing industry where historically the size of a printer point was approximately 1/72 of an inch. Quartz has adopted the same definition of a point as the PostScript and PDF imaging models with 1 point being exactly 1/72 of an inch.

Because Quartz drawing calls take user space coordinates as parameters and the output device coordinates are in device space, Quartz must map all user space coordinates into device space coordinates as part of its rendering. By providing an abstract user space coordinate system and taking care of the mapping of those coordinates onto the output device, Quartz provides a device-independent coor- dinate system. The coordinate mapping performed by Quartz also provides the flexibility of additional user space transformations, such as the translation, rota- tion, and scaling of coordinates as seen in the examples in “Quartz 2D Drawing Basics” (page 15).



current transformation matrix    // CTM
CGAffineTransform   //   This data structure consists of six floating-point values: a, b, c, d, tx, and ty.

x′ = a * x + c * y + tx
y′ b * x + d * y + ty

一个不对称的坐标缩放,接上一个旋转会制造一个倾斜的坐标系  Nonuniform scaling of a coordinate system, followed by a rotation, produces a skew or shear to the coordinate axes


// alpha is 22.5 degrees and beta is 15 degrees. 
float alpha = M_PI/8, beta = M_PI/12;

CGAffineTransform skew = CGAffineTransformMake(1, tan(alpha), tan(beta), 1, 0, 0);

CGContextConcatCTM(context, skew);

所有的Quartz绘画分为3类, 画线,图像, 文字。  All Quartz drawing falls into of one of three fundamental categories: line art (graphics that consist of paths that are filled, stroked, or both), sampled images, and text.


quadratic 二次
cubic 立方体

Path  可以是open,  closed, 有方向(direction)

一个Path可以有许多subpath 这些subpath未必相连, CGContextMoveToPoint 就创建了一个subpath

    CGContextSetRGBStrokeColor(context, 0, 0, 1, 0.7);

    CGContextBeginPath(context);

    

    CGContextMoveToPoint(context, 0, 0);

    CGContextAddLineToPoint(context, 30, 40);

    

    CGContextMoveToPoint(context, -20, 10);

    CGContextAddLineToPoint(context, 100, 70);

   

    CGContextDrawPath(context, kCGPathStroke); 


所以的path都由下面5种基本方法创建  All paths in Quartz can be constructed using one or more of the following five basic path construction primitive functions,

CGContextMoveToPoint begins a new subpath in the current path.

CGContextAddLineToPoint adds a straight line segment to the current path.

CGContextAddCurveToPoint adds a cubic Bézier curve segment to the current path.

CGContextAddQuadCurveToPoint adds a quadratic Bézier curve segment to the current path.

CGContextClosePath ends the current path.  // 在当前点和起点之间画一条线


CGContextBeginPath.   clear path


Cubic Bézier curves are defined by two endpoints together with two additional control points.
P(t) = (1 – t)3P0 + 3t (1  t)2C1 + 3t2(1 – t)C2 + t3P1

CGContextAddCurveToPoint(context, c1.x, c1.y, c2.x, c2.y, p1.x, p1.y);

quadratic Bézier curve are defined by two endpoints and a single control point
P(t) = (1  t)2P0 + 2t(1 – t)C + t2P1
CGContextAddQuadCurveToPoint(<#CGContextRef c#>, <#CGFloat cpx#>, <#CGFloat cpy#>, <#CGFloat x#>, <#CGFloat y#>)



CGContextClosePath 执行的时候看起点和最后一点是不是在一起, 不在一起的话会添加一条线把她们连起来。 Quartz subpaths are either open or closed. A closed subpath has its initial point connected to the last point on the subpath. The function CGContextClosePath connects the last point on the subpath with the initial point on the subpath


CGContextAddRect
CGContextAddRects
CGContextAddLines
CGContextAddArc   //The result- ing subpath is open; you must call CGContextClosePath if you want to close it. 如果之前有点的话, 会在此点和圆的起点连一条线 
CGContextAddEllipseInRect
CGContextFillEllipseInRect
CGContextStrokeEllipseInRect
CGContextStrokeLineSegments
CGContextStrokeRect
CGContextStrokeRectWithWidth
CGContextFillRect
CGContextFillRects
CGContextClipToRect
CGContextClipToRects



line width    //line width is affected by the scaling aspects of the CTM
line join   //  three different types of joins—miter, round, or bevel        kCGLineJoinMiter, kCGLineJoinRound, or kCG- LineJoinBevel

line cap    // butt, square, or rounded    kCGLineCap- Butt, kCGLineCapSquare, or kCGLineCapRound

line dash



Filling a path
Filling 是填充path的内部, 但复杂path的内部不好判断. Quartz defines two distinct rules to determine the interior of a path. You choose which rule to apply when filling a path—the non- zero winding number rule or the even-odd (kCGPathEOFillStroke) rule  


CGPathRef

CGMutablePathRef path = CGPathCreateMutable();

CGPathAddArc(path, &theTransform, 0., 0., 45., 0., 2*M_PI, false); 
CGPathCloseSubpath(path);

utility
CGContextSetAllowsAntialiasing
CGContextGetPathBoundingBox
CGRectNull
CGRectEqualToRect
CGContextIsPathEmpty
CGContextGetPathCurrentPoint
CGPointZero
CGContextReplacePathWithStrokedPath
CGContextPathContainsPoint     // 看点在path内不


Color  包括颜色组件,透明度     还有全局透明度 //Quartz supports a global alpha, applied to all drawing
graphics state
  Fill and stroke colors
  current transformation matrix (CTM)
  the clipping area, the font, and more than a dozen other parameters

Color Spaces 
    RGB uses three—red, green, and blue
     CMYK uses four—cyan, magenta, yellow, and black

Quartz color spaces fall into three basic categories
  Calibrated color spaces specify color  is reproduc- ible across a wide range of output devices.  ICCBased, CalibratedGray, CalibratedRGB, and Lab color spaces.
   Device-dependent color spaces      DeviceGray, DeviceRGB, and DeviceCMYK color spaces
   Special color spaces      Pattern color space   Indexed color space


intrinsic自带

Quartz also uses the current fill color space and fill color component values when painting images without intrinsic color
 
CGContextSetRGBFillColor implicitly uses the DeviceRGB color space 

CGColorSpaceRef
CGColorRef

CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();

float opaqueRed[] = {0.663, 0.0, 0.031, 1.0}

CGContextSetFillColorSpace(context, theColorSpace);

CGContextSetFillColor(context, opaqueRed);


Fill alpha = 0.25 
Global alpha = 0.5 
Effective alpha = .125


CGDataProviderCreateWithURL
CGDataProviderCreateWithData
CGDataProviderCreate
CGDataProviderCreateDirectAccess
CGDataProviderCreateWithCFData

CGImageCreateWithJPEGDataProvider
CGImageCreateWithPNGDataProvider
CGImageCreate
CGImageSourceCreateWithDataProvider
CGImageSourceCreateWithData
CGImageSourceCreateWithURL
GraphicsImportCreateCGImage


CFURLRef url = …..;
CGRect jpgRect;
CGImageRef jpgImage = NULL;
CGDataProviderRef jpgProvider = CGDataProviderCreateWithURL(url);
jpgImage = CGImageCreateWithJPEGDataProvider(jpgProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(jpgProvider);
jpgRect = CGRectMake(0., 0., CGImageGetWidth(jpgImage)/4, CGImageGetHeight(jpgImage)/4);
CGContextDrawImage(context, jpgRect, jpgImage);


image mask, masking image, and mask
stencil 模板
image mask 就是模板图片  就象在白纸上盖上模板, 然后喷漆,纸上就会留下印迹
The terms image mask, masking image, and mask are interchange- able terms to describe an image whose sample values indicate a percentage of paint to apply but not the color of the paint itself. Such an image is sometimes called a stencil mask because the mask does not itself have any intrinsic color; instead, color “pours” through the stencil. An image mask has only one compo- nent value, the coverage value.

1bit mask只显示on/off,  8bit可以显示色深,
An image mask can be 1, 2, 4, or 8 bits per sample. A sample value that decodes to allows paint to go through it—it’s the “hole” in the stencil. A sample value that decodes to doesn’t allow paint through—it’s the solid part of the stencil. A 1-bit mask, by definition, has only “on/off” options—0 or 1. Deeper masks, such as an 8-bit mask, can contain intermediate values (0 < 1) that specify grada- tions of paint that get through the mask, with lower values allowing more paint than higher values.

Quartz provides three masking devices that can control how pixels are painted— image masks, images that are used for the purpose of masking another image, and masking colors.


Text
CGContextSelectFont     sets both the font and the font size
CGContextSetFont         sets only the font

text space        Quartz text is drawn in a special coordinate system called text space
text matrix       an affine transform that maps text space coordinates into user space coordinates
0
0
分享到:
评论

相关推荐

    Quartz 2D编程指南

    《Quartz 2D编程指南》一文详细介绍了Quartz 2D这一核心绘图引擎在iOS和其他Apple平台上的应用及特性。Quartz 2D是Apple提供的底层绘图API,用于创建高质量的二维图形,它不仅适用于iOS,还广泛应用于macOS、watchOS...

    4个案例:Quartz2D图片处理

    Quartz2D是苹果操作系统内核中的一个二维图形渲染引擎,它为开发者提供了一套强大的API,用于在iOS和macOS平台上进行低级的图形绘制和图像处理。本篇文章将通过四个具体的案例来深入理解Quartz2D在图片处理方面的...

    Programming with Quartz 2D and PDF Graphics in MacOS

    ### 编程与Quartz 2D及PDF图形在MacOS中的应用 #### 一、Quartz 2D概述 Quartz 2D是苹果公司为MacOS开发的一个高性能、功能丰富的2D图形处理框架。它不仅适用于MacOS平台,也被广泛应用于iOS设备上。Quartz 2D提供...

    Quartz2D的基本使用

    Quartz2D是苹果操作系统(包括macOS和iOS)中的一个强大的2D图形绘制框架,它允许开发者创建高质量的矢量图形、文本和图像。在本文中,我们将深入探讨Quartz2D的基本使用,了解其核心概念和常用API,以及如何在实际...

    Programming with Quartz 2D and PDF Graphics in Mac OS X

    ### 关于《使用Quartz 2D与PDF图形在Mac OS X编程》的知识点解析 #### 一、Quartz 2D与PDF图形基础 **Quartz 2D**是苹果为Mac OS X和iOS提供的核心图形处理框架之一。它为开发者提供了高级的2D绘图功能,包括路径...

    Quartz 2D编程指南 官方文档翻译

    Quartz 2D是苹果公司开发的一个先进的二维绘图引擎,它的API在iOS、tvOS和macOS的应用程序开发中广泛使用。Quartz 2D的主要特点包括其低级轻量级的二维渲染能力,无与伦比的输出保真度,以及独立于分辨率和设备的...

    Quartz2D动画Demo

    Quartz2D是苹果iOS和Mac OS X操作系统中用于图形绘制和动画的一种强大的技术,它基于Core Graphics框架。在这个"Quartz2D动画Demo"中,我们将会探讨如何利用Quartz2D来创建动态视觉效果,这对于iOS应用的用户体验...

    quartz2D绘图代码(使用路径)

    Quartz 2D是苹果操作系统中的核心图形库,用于在iOS和macOS平台上进行低级2D绘图。它提供了一套丰富的API,允许开发者直接控制像素级别的绘图,实现精细的图形渲染。在Quartz 2D中,路径(CGPath)是一个强大的工具...

    Quartz 2D Programming Guide

    ### Quartz 2D编程指南知识点概述 #### 一、Quartz 2D 概述 - **Quartz 2D** 是苹果公司在Mac OS X和iOS操作系统中提供的一套图形处理框架,它允许开发者创建复杂的二维图形。Quartz 2D 提供了一系列的功能,包括...

    Quartz 2D编程指南(1)

    ### Quartz 2D编程指南(1) - 概览 #### 一、Quartz 2D简介 Quartz 2D 是苹果公司提供的一套强大的二维图形绘制引擎,广泛应用于 iOS 和 macOS 平台。它为开发者提供了丰富的 API 接口,支持多种图形绘制和图像处理...

    Quartz2D图形刷新和定时器

    Quartz2D是苹果操作系统中的一个强大的2D图形绘制框架,它允许开发者创建高质量的图形、图像和文本。在iOS和macOS应用开发中,Quartz2D被广泛用于实现自定义视图、动画和图形界面。本篇将深入探讨Quartz2D图形的刷新...

    Quartz2D的各种图形的绘制

    Quartz2D是苹果操作系统(包括iOS和macOS)中的核心图形库,它提供了一套强大的2D绘图API,允许开发者创建出高质量、高分辨率的图像和图形。本教程将深入探讨Quartz2D的基本概念,以及如何利用它来绘制各种形状。 ...

    ios Quartz 2D编程指南

    ios Quartz 2D编程指南 2d图形处理

    Quartz 2D Demo @appleDev

    Quartz 2D是Apple开发的一种二维图形渲染技术,它为iOS和macOS平台提供了强大的图形绘制能力。在“Quartz 2D Demo @appleDev”这个项目中,我们可以深入理解并学习到如何利用Apple的Quartz 2D API来创建高质量、高...

    Quartz 2D programing 翻译

    Quartz 2D是苹果公司为其iOS和Mac OS X操作系统开发的一个2D图形绘制引擎。它提供了一套底层而轻量级的API接口,允许开发者在不同输出设备上生成逼真的图形效果,同时保持与设备和分辨率的无关性。这意味着开发者...

    Quartz 2D编程指南(英文完整版+中文部分翻译版)

    Quartz 2D是苹果平台上的2D图形绘制框架,用于在iOS和Mac OS系统上进行高质量的图形渲染。这份编程指南提供了丰富的信息,帮助开发者深入理解并掌握Quartz 2D的使用方法。以下是对Quartz 2D编程指南中的关键知识点的...

    iOS开发之Quartz2D的介绍与使用详解

    一、前言 Quartz2D的API是纯C语言...其实,iOS中大部分控件的内容都是通过Quartz2D画出来的,因此,Quartz2D在iOS开发中很重要的一个价值是:自定义view(自定义UI控件)。 Quartz 2D能完成的工作: 绘制图形 : 线条\

    Quartz 2D Programming Guide 2007

    Quartz 2D是苹果公司开发的一个强大的二维图形渲染引擎,它被广泛应用于Mac OS X和iOS操作系统中,为开发者提供了高效、灵活的图形绘制能力。"Quartz 2D Programming Guide 2007"是一份详尽的文档,旨在帮助开发者...

    Quartz2D使用案例

    Quartz2D是苹果平台上的一个强大的2D图形渲染框架,它被广泛应用于iOS和macOS应用程序开发中,用于创建高质量的图形、图像处理和动画。这个“Quartz2D使用案例”代码库提供了一些实用的示例,帮助开发者更好地理解和...

Global site tag (gtag.js) - Google Analytics