副标题: 2D and PDF Graphics in Mac OS X (The Morgan Kaufmann Series in Computer Graphics)
作者: David Gelphman / Bunny Laden
出版社: Morgan Kaufmann
出版年: 2005-12-28
页数: 668
定价: USD 86.95
装帧: Paperback
ISBN: 9780123694737
作者: David Gelphman / Bunny Laden
出版社: Morgan Kaufmann
出版年: 2005-12-28
页数: 668
定价: USD 86.95
装帧: Paperback
ISBN: 9780123694737
内容简介 · · · · · ·
Written by members of the development team at Apple, Programming with Quartz is the first book to describe the sophisticated graphics system of Mac OS X. By using the methods described in this book, developers will be able to fully exploit the state-of-the-art graphics capabilities of Mac OS X in their applications, whether for Cocoa or Carbon development. This book also serves... (展开全部)
Written by members of the development team at Apple, Programming with Quartz is the first book to describe the sophisticated graphics system of Mac OS X. By using the methods described in this book, developers will be able to fully exploit the state-of-the-art graphics capabilities of Mac OS X in their applications, whether for Cocoa or Carbon development. This book also serves as an introduction to 2D graphics concepts, including how images are drawn and how color is rendered. It includes guidance for working with PDF documents, drawing bitmap graphics, using Quartz built-in color management, and drawing text. Programming with Quartz is a rich resource for new and experienced Mac OS X developers, Cocoa and Carbon programmers, UNIX developers who are migrating to Mac OS X, and anyone interested in powerful 2D graphics systems. * This is the definitive guide to the revolutionary graphics system of Mac OS X that uses the Portable Document Format (PDF) as the basis of its imaging model.* It contains the latest on programming with Quartz for Mac OS X version 10.4.* Carefully crafted and extensive code examples show how to accomplish most of the drawing tasks possible with Quartz.
豆瓣成员常用的标签(共8个) · · · · · ·
按有用程度 按页码先后 最新笔记
-
第1页
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) // 设置颜色 CGC... (更多)Context 上下文 画板 可以是window printer bitmap 显示屏Filling 填充alpha (opacity) 不透明度 1为不透明 0透明 此属性决定能否看到下面的画面opaque 不透明stroking 画边框 在rectangle的边框(此边框无限thin)2侧画线 线有宽度CGContextRef context = UIGraphicsGetCurrentContext(); // drawRect 方法中拿到contextCGContextSetRGBFillColor(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侧的 所以画完框在填充 会把内侧的边框覆盖掉CGContextTranslateCTMCGContextScaleCTMCGContextRotateCTMarbitrary 任意的path 任何图像都是由path构成的a sequence of lines and curves that make up a shape, and then performing painting operations on that pathCGContextBeginPath 用空白路径替换context中已存在路径 replaces any existing path in the context with an empty pathCGContextMoveToPoint path的起点 establishes the first point on the path and makes that the current pointCGContextAddLineToPoint 画线 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 pathCGContextDrawPath path不会产生任何图像 draw才会,同时这个方法也会清除path。 actually paint the stroked, filled, and stroked-and-filled paths, also clears the path from the contextkCGPathFill kCGPathStroke kCGPathFillStroke //This type of fill-then-stroke operation is so common that Quartz defines the special kCGPathFillStroke painting mode used hereCGContextTranslateCTM(context, 200.0, 0.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.0, 6.0, 5.0, 6.0, 5.0, 6.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 CGRectCGContextAddArc(context, circleCenter.x, circleCenter.y, circleRadius, startingAngle, endingAngle, 0); 画圆, 参数:圆心,半径, 起始角度,终点角度, 最后一个应该是否顺时针CGContextScaleCTM(context, 1, -1); 放缩坐标 -1是倒转坐标Y轴Window contextBitmap graphics contextPDF graphics contextPostScript contextGLContext contextYou 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 documentUser Space and Device SpaceFor 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 // CTMCGAffineTransform // This data structure consists of six floating-point values: a, b, c, d, tx, and ty.x′ = a * x + c * y + txy′ = 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,I CGContextMoveToPoint begins a new subpath in the current path.I CGContextAddLineToPoint adds a straight line segment to the current path.I CGContextAddCurveToPoint adds a cubic Bézier curve segment to the current path.I CGContextAddQuadCurveToPoint adds a quadratic Bézier curve segment to the current path.I CGContextClosePath ends the current path. // 在当前点和起点之间画一条线CGContextBeginPath. clear pathCubic 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 + t3P1CGContextAddCurveToPoint(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 pointP(t) = (1 – t)2P0 + 2t(1 – t)C + t2P1CGContextAddQuadCurveToPoint(<#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 subpathCGContextAddRectCGContextAddRectsCGContextAddLinesCGContextAddArc //The result- ing subpath is open; you must call CGContextClosePath if you want to close it. 如果之前有点的话, 会在此点和圆的起点连一条线 CGContextAddEllipseInRectCGContextFillEllipseInRectCGContextStrokeEllipseInRectCGContextStrokeLineSegmentsCGContextStrokeRectCGContextStrokeRectWithWidthCGContextFillRectCGContextFillRectsCGContextClipToRectCGContextClipToRectsline width //line width is affected by the scaling aspects of the CTMline join // three different types of joins—miter, round, or bevel kCGLineJoinMiter, kCGLineJoinRound, or kCG- LineJoinBevelline cap // butt, square, or rounded kCGLineCap- Butt, kCGLineCapSquare, or kCGLineCapRoundline dashFilling a pathFilling 是填充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 CGPathRefCGMutablePathRef path = CGPathCreateMutable();CGPathAddArc(path, &theTransform, 0., 0., 45., 0., 2*M_PI, false); CGPathCloseSubpath(path);utilityCGContextSetAllowsAntialiasingCGContextGetPathBoundingBoxCGRectNullCGRectEqualToRectCGContextIsPathEmptyCGContextGetPathCurrentPointCGPointZeroCGContextReplacePathWithStrokedPathCGContextPathContainsPoint // 看点在path内不Color 包括颜色组件,透明度 还有全局透明度 //Quartz supports a global alpha, applied to all drawinggraphics state Fill and stroke colors current transformation matrix (CTM) the clipping area, the font, and more than a dozen other parametersColor Spaces RGB uses three—red, green, and blue CMYK uses four—cyan, magenta, yellow, and blackQuartz 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 spaceintrinsic自带Quartz also uses the current fill color space and fill color component values when painting images without intrinsic colorCGContextSetRGBFillColor implicitly uses the DeviceRGB color space CGColorSpaceRefCGColorRefCGColorSpaceRef 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 = .125CGDataProviderCreateWithURLCGDataProviderCreateWithDataCGDataProviderCreateCGDataProviderCreateDirectAccessCGDataProviderCreateWithCFDataCGImageCreateWithJPEGDataProviderCGImageCreateWithPNGDataProviderCGImageCreateCGImageSourceCreateWithDataProviderCGImageSourceCreateWithDataCGImageSourceCreateWithURLGraphicsImportCreateCGImageCFURLRef 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 maskstencil 模板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 0 allows paint to go through it—it’s the “hole” in the stencil. A sample value that decodes to 1 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 < x < 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.TextCGContextSelectFont sets both the font and the font sizeCGContextSetFont sets only the fonttext space Quartz text is drawn in a special coordinate system called text spacetext matrix an affine transform that maps text space coordinates into user space coordinates (收起)2011-10-10 15:16:48 回应
-
第1页
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) // 设置颜色 CGC... (更多)Context 上下文 画板 可以是window printer bitmap 显示屏Filling 填充alpha (opacity) 不透明度 1为不透明 0透明 此属性决定能否看到下面的画面opaque 不透明stroking 画边框 在rectangle的边框(此边框无限thin)2侧画线 线有宽度CGContextRef context = UIGraphicsGetCurrentContext(); // drawRect 方法中拿到contextCGContextSetRGBFillColor(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侧的 所以画完框在填充 会把内侧的边框覆盖掉CGContextTranslateCTMCGContextScaleCTMCGContextRotateCTMarbitrary 任意的path 任何图像都是由path构成的a sequence of lines and curves that make up a shape, and then performing painting operations on that pathCGContextBeginPath 用空白路径替换context中已存在路径 replaces any existing path in the context with an empty pathCGContextMoveToPoint path的起点 establishes the first point on the path and makes that the current pointCGContextAddLineToPoint 画线 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 pathCGContextDrawPath path不会产生任何图像 draw才会,同时这个方法也会清除path。 actually paint the stroked, filled, and stroked-and-filled paths, also clears the path from the contextkCGPathFill kCGPathStroke kCGPathFillStroke //This type of fill-then-stroke operation is so common that Quartz defines the special kCGPathFillStroke painting mode used hereCGContextTranslateCTM(context, 200.0, 0.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.0, 6.0, 5.0, 6.0, 5.0, 6.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 CGRectCGContextAddArc(context, circleCenter.x, circleCenter.y, circleRadius, startingAngle, endingAngle, 0); 画圆, 参数:圆心,半径, 起始角度,终点角度, 最后一个应该是否顺时针CGContextScaleCTM(context, 1, -1); 放缩坐标 -1是倒转坐标Y轴Window contextBitmap graphics contextPDF graphics contextPostScript contextGLContext contextYou 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 documentUser Space and Device SpaceFor 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 // CTMCGAffineTransform // This data structure consists of six floating-point values: a, b, c, d, tx, and ty.x′ = a * x + c * y + txy′ = 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,I CGContextMoveToPoint begins a new subpath in the current path.I CGContextAddLineToPoint adds a straight line segment to the current path.I CGContextAddCurveToPoint adds a cubic Bézier curve segment to the current path.I CGContextAddQuadCurveToPoint adds a quadratic Bézier curve segment to the current path.I CGContextClosePath ends the current path. // 在当前点和起点之间画一条线CGContextBeginPath. clear pathCubic 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 + t3P1CGContextAddCurveToPoint(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 pointP(t) = (1 – t)2P0 + 2t(1 – t)C + t2P1CGContextAddQuadCurveToPoint(<#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 subpathCGContextAddRectCGContextAddRectsCGContextAddLinesCGContextAddArc //The result- ing subpath is open; you must call CGContextClosePath if you want to close it. 如果之前有点的话, 会在此点和圆的起点连一条线 CGContextAddEllipseInRectCGContextFillEllipseInRectCGContextStrokeEllipseInRectCGContextStrokeLineSegmentsCGContextStrokeRectCGContextStrokeRectWithWidthCGContextFillRectCGContextFillRectsCGContextClipToRectCGContextClipToRectsline width //line width is affected by the scaling aspects of the CTMline join // three different types of joins—miter, round, or bevel kCGLineJoinMiter, kCGLineJoinRound, or kCG- LineJoinBevelline cap // butt, square, or rounded kCGLineCap- Butt, kCGLineCapSquare, or kCGLineCapRoundline dashFilling a pathFilling 是填充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 CGPathRefCGMutablePathRef path = CGPathCreateMutable();CGPathAddArc(path, &theTransform, 0., 0., 45., 0., 2*M_PI, false); CGPathCloseSubpath(path);utilityCGContextSetAllowsAntialiasingCGContextGetPathBoundingBoxCGRectNullCGRectEqualToRectCGContextIsPathEmptyCGContextGetPathCurrentPointCGPointZeroCGContextReplacePathWithStrokedPathCGContextPathContainsPoint // 看点在path内不Color 包括颜色组件,透明度 还有全局透明度 //Quartz supports a global alpha, applied to all drawinggraphics state Fill and stroke colors current transformation matrix (CTM) the clipping area, the font, and more than a dozen other parametersColor Spaces RGB uses three—red, green, and blue CMYK uses four—cyan, magenta, yellow, and blackQuartz 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 spaceintrinsic自带Quartz also uses the current fill color space and fill color component values when painting images without intrinsic colorCGContextSetRGBFillColor implicitly uses the DeviceRGB color space CGColorSpaceRefCGColorRefCGColorSpaceRef 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 = .125CGDataProviderCreateWithURLCGDataProviderCreateWithDataCGDataProviderCreateCGDataProviderCreateDirectAccessCGDataProviderCreateWithCFDataCGImageCreateWithJPEGDataProviderCGImageCreateWithPNGDataProviderCGImageCreateCGImageSourceCreateWithDataProviderCGImageSourceCreateWithDataCGImageSourceCreateWithURLGraphicsImportCreateCGImageCFURLRef 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 maskstencil 模板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 0 allows paint to go through it—it’s the “hole” in the stencil. A sample value that decodes to 1 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 < x < 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.TextCGContextSelectFont sets both the font and the font sizeCGContextSetFont sets only the fonttext space Quartz text is drawn in a special coordinate system called text spacetext matrix an affine transform that maps text space coordinates into user space coordinates (收起)2011-10-10 15:16:48 回应
-
第1页
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) // 设置颜色 CGC... (更多)Context 上下文 画板 可以是window printer bitmap 显示屏Filling 填充alpha (opacity) 不透明度 1为不透明 0透明 此属性决定能否看到下面的画面opaque 不透明stroking 画边框 在rectangle的边框(此边框无限thin)2侧画线 线有宽度CGContextRef context = UIGraphicsGetCurrentContext(); // drawRect 方法中拿到contextCGContextSetRGBFillColor(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侧的 所以画完框在填充 会把内侧的边框覆盖掉CGContextTranslateCTMCGContextScaleCTMCGContextRotateCTMarbitrary 任意的path 任何图像都是由path构成的a sequence of lines and curves that make up a shape, and then performing painting operations on that pathCGContextBeginPath 用空白路径替换context中已存在路径 replaces any existing path in the context with an empty pathCGContextMoveToPoint path的起点 establishes the first point on the path and makes that the current pointCGContextAddLineToPoint 画线 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 pathCGContextDrawPath path不会产生任何图像 draw才会,同时这个方法也会清除path。 actually paint the stroked, filled, and stroked-and-filled paths, also clears the path from the contextkCGPathFill kCGPathStroke kCGPathFillStroke //This type of fill-then-stroke operation is so common that Quartz defines the special kCGPathFillStroke painting mode used hereCGContextTranslateCTM(context, 200.0, 0.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.0, 6.0, 5.0, 6.0, 5.0, 6.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 CGRectCGContextAddArc(context, circleCenter.x, circleCenter.y, circleRadius, startingAngle, endingAngle, 0); 画圆, 参数:圆心,半径, 起始角度,终点角度, 最后一个应该是否顺时针CGContextScaleCTM(context, 1, -1); 放缩坐标 -1是倒转坐标Y轴Window contextBitmap graphics contextPDF graphics contextPostScript contextGLContext contextYou 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 documentUser Space and Device SpaceFor 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 // CTMCGAffineTransform // This data structure consists of six floating-point values: a, b, c, d, tx, and ty.x′ = a * x + c * y + txy′ = 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,I CGContextMoveToPoint begins a new subpath in the current path.I CGContextAddLineToPoint adds a straight line segment to the current path.I CGContextAddCurveToPoint adds a cubic Bézier curve segment to the current path.I CGContextAddQuadCurveToPoint adds a quadratic Bézier curve segment to the current path.I CGContextClosePath ends the current path. // 在当前点和起点之间画一条线CGContextBeginPath. clear pathCubic 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 + t3P1CGContextAddCurveToPoint(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 pointP(t) = (1 – t)2P0 + 2t(1 – t)C + t2P1CGContextAddQuadCurveToPoint(<#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 subpathCGContextAddRectCGContextAddRectsCGContextAddLinesCGContextAddArc //The result- ing subpath is open; you must call CGContextClosePath if you want to close it. 如果之前有点的话, 会在此点和圆的起点连一条线 CGContextAddEllipseInRectCGContextFillEllipseInRectCGContextStrokeEllipseInRectCGContextStrokeLineSegmentsCGContextStrokeRectCGContextStrokeRectWithWidthCGContextFillRectCGContextFillRectsCGContextClipToRectCGContextClipToRectsline width //line width is affected by the scaling aspects of the CTMline join // three different types of joins—miter, round, or bevel kCGLineJoinMiter, kCGLineJoinRound, or kCG- LineJoinBevelline cap // butt, square, or rounded kCGLineCap- Butt, kCGLineCapSquare, or kCGLineCapRoundline dashFilling a pathFilling 是填充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 CGPathRefCGMutablePathRef path = CGPathCreateMutable();CGPathAddArc(path, &theTransform, 0., 0., 45., 0., 2*M_PI, false); CGPathCloseSubpath(path);utilityCGContextSetAllowsAntialiasingCGContextGetPathBoundingBoxCGRectNullCGRectEqualToRectCGContextIsPathEmptyCGContextGetPathCurrentPointCGPointZeroCGContextReplacePathWithStrokedPathCGContextPathContainsPoint // 看点在path内不Color 包括颜色组件,透明度 还有全局透明度 //Quartz supports a global alpha, applied to all drawinggraphics state Fill and stroke colors current transformation matrix (CTM) the clipping area, the font, and more than a dozen other parametersColor Spaces RGB uses three—red, green, and blue CMYK uses four—cyan, magenta, yellow, and blackQuartz 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 spaceintrinsic自带Quartz also uses the current fill color space and fill color component values when painting images without intrinsic colorCGContextSetRGBFillColor implicitly uses the DeviceRGB color space CGColorSpaceRefCGColorRefCGColorSpaceRef 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 = .125CGDataProviderCreateWithURLCGDataProviderCreateWithDataCGDataProviderCreateCGDataProviderCreateDirectAccessCGDataProviderCreateWithCFDataCGImageCreateWithJPEGDataProviderCGImageCreateWithPNGDataProviderCGImageCreateCGImageSourceCreateWithDataProviderCGImageSourceCreateWithDataCGImageSourceCreateWithURLGraphicsImportCreateCGImageCFURLRef 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 maskstencil 模板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 0 allows paint to go through it—it’s the “hole” in the stencil. A sample value that decodes to 1 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 < x < 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.TextCGContextSelectFont sets both the font and the font sizeCGContextSetFont sets only the fonttext space Quartz text is drawn in a special coordinate system called text spacetext matrix an affine transform that maps text space coordinates into user space coordinates (收起)2011-10-10 15:16:48 回应
"Programming with Quartz"的论坛 · · · · · ·
| 了解Mac OS X图形编程的好书 | 来自牛仔裤恤衫 | 2012-01-13 |
在哪儿买这本书? · · · · · ·
- 亚马逊 (RMB 608.70)
- 查看1家网店价格 (608.70元起)
- 加入购书单 多本比价 批量购买 已在购书单
- > 点这儿转让 有11人想读,手里有一本闲着?
以下豆列推荐 · · · · · ·
- Mac Development Books (最后的独角兽)
- The Morgan Kaufmann Series in Computer Graphics (Sail Tsao)
- Buy for my Kindle (野孩子)
谁读这本书?
订阅关于Programming with Quartz的评论:
feed: rss 2.0

