---
title: CGContext
framework: coregraphics
role: symbol
role_heading: Class
path: coregraphics/cgcontext
---

# CGContext

A Quartz 2D drawing environment.

## Declaration

```swift
class CGContext
```

## Overview

Overview A CGContext instance represents a Quartz 2D drawing destination. A graphics context contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap image, a PDF document, or a printer.

## Topics

### Creating Bitmap Graphics Contexts

- [CGBitmapContextReleaseDataCallback](coregraphics/cgbitmapcontextreleasedatacallback.md)

### Creating PDF Graphics Contexts

- [init(_:mediaBox:_:)](coregraphics/cgcontext/init(_:mediabox:_:).md)
- [init(consumer:mediaBox:_:)](coregraphics/cgcontext/init(consumer:mediabox:_:).md)
- [Auxiliary Dictionary Keys](coregraphics/auxiliary-dictionary-keys.md)

### Converting Between Coordinate Spaces

- [userSpaceToDeviceSpaceTransform](coregraphics/cgcontext/userspacetodevicespacetransform.md)
- [convertToDeviceSpace(_:)](coregraphics/cgcontext/converttodevicespace(_:)-53m7u.md)
- [convertToUserSpace(_:)](coregraphics/cgcontext/converttouserspace(_:)-3mtg3.md)
- [convertToDeviceSpace(_:)](coregraphics/cgcontext/converttodevicespace(_:)-91x5g.md)
- [convertToUserSpace(_:)](coregraphics/cgcontext/converttouserspace(_:)-1hk5r.md)
- [convertToDeviceSpace(_:)](coregraphics/cgcontext/converttodevicespace(_:)-224h2.md)
- [convertToUserSpace(_:)](coregraphics/cgcontext/converttouserspace(_:)-693ur.md)

### Constructing a Current Graphics Path

- [beginPath()](coregraphics/cgcontext/beginpath().md)
- [move(to:)](coregraphics/cgcontext/move(to:).md)
- [addLine(to:)](coregraphics/cgcontext/addline(to:).md)
- [addLines(between:)](coregraphics/cgcontext/addlines(between:).md)
- [addRect(_:)](coregraphics/cgcontext/addrect(_:).md)
- [addRects(_:)](coregraphics/cgcontext/addrects(_:).md)
- [addEllipse(in:)](coregraphics/cgcontext/addellipse(in:).md)
- [addArc(center:radius:startAngle:endAngle:clockwise:)](coregraphics/cgcontext/addarc(center:radius:startangle:endangle:clockwise:).md)
- [addArc(tangent1End:tangent2End:radius:)](coregraphics/cgcontext/addarc(tangent1end:tangent2end:radius:).md)
- [addCurve(to:control1:control2:)](coregraphics/cgcontext/addcurve(to:control1:control2:).md)
- [addQuadCurve(to:control:)](coregraphics/cgcontext/addquadcurve(to:control:).md)
- [addPath(_:)](coregraphics/cgcontext/addpath(_:).md)
- [closePath()](coregraphics/cgcontext/closepath().md)
- [path](coregraphics/cgcontext/path.md)
- [replacePathWithStrokedPath()](coregraphics/cgcontext/replacepathwithstrokedpath().md)

### Examining the Current Graphics Path

- [boundingBoxOfPath](coregraphics/cgcontext/boundingboxofpath.md)
- [currentPointOfPath](coregraphics/cgcontext/currentpointofpath.md)
- [isPathEmpty](coregraphics/cgcontext/ispathempty.md)
- [pathContains(_:mode:)](coregraphics/cgcontext/pathcontains(_:mode:).md)

### Drawing the Current Graphics Path

- [drawPath(using:)](coregraphics/cgcontext/drawpath(using:).md)
- [CGPathDrawingMode](coregraphics/cgpathdrawingmode.md)
- [fillPath(using:)](coregraphics/cgcontext/fillpath(using:).md)
- [strokePath()](coregraphics/cgcontext/strokepath().md)

### Drawing Shapes

- [clear(_:)](coregraphics/cgcontext/clear(_:).md)
- [fill(_:)](coregraphics/cgcontext/fill(_:)-7a0rk.md)
- [fill(_:)](coregraphics/cgcontext/fill(_:)-6jc4y.md)
- [fillEllipse(in:)](coregraphics/cgcontext/fillellipse(in:).md)
- [stroke(_:)](coregraphics/cgcontext/stroke(_:).md)
- [stroke(_:width:)](coregraphics/cgcontext/stroke(_:width:).md)
- [strokeEllipse(in:)](coregraphics/cgcontext/strokeellipse(in:).md)
- [strokeLineSegments(between:)](coregraphics/cgcontext/strokelinesegments(between:).md)

### Drawing Images and PDF Content

- [draw(_:in:byTiling:)](coregraphics/cgcontext/draw(_:in:bytiling:).md)
- [drawPDFPage(_:)](coregraphics/cgcontext/drawpdfpage(_:).md)
- [interpolationQuality](coregraphics/cgcontext/interpolationquality.md)
- [CGInterpolationQuality](coregraphics/cginterpolationquality.md)

### Drawing Gradients and Shadings

- [drawLinearGradient(_:start:end:options:)](coregraphics/cgcontext/drawlineargradient(_:start:end:options:).md)
- [drawRadialGradient(_:startCenter:startRadius:endCenter:endRadius:options:)](coregraphics/cgcontext/drawradialgradient(_:startcenter:startradius:endcenter:endradius:options:).md)
- [CGGradientDrawingOptions](coregraphics/cggradientdrawingoptions.md)
- [drawShading(_:)](coregraphics/cgcontext/drawshading(_:).md)

### Drawing Text

- [textMatrix](coregraphics/cgcontext/textmatrix.md)
- [textPosition](coregraphics/cgcontext/textposition.md)
- [selectFont(name:size:textEncoding:)](coregraphics/cgcontext/selectfont(name:size:textencoding:).md)
- [setCharacterSpacing(_:)](coregraphics/cgcontext/setcharacterspacing(_:).md)
- [setFont(_:)](coregraphics/cgcontext/setfont(_:).md)
- [setFontSize(_:)](coregraphics/cgcontext/setfontsize(_:).md)
- [setTextDrawingMode(_:)](coregraphics/cgcontext/settextdrawingmode(_:).md)
- [setAllowsFontSmoothing(_:)](coregraphics/cgcontext/setallowsfontsmoothing(_:).md)
- [setAllowsFontSubpixelPositioning(_:)](coregraphics/cgcontext/setallowsfontsubpixelpositioning(_:).md)
- [setAllowsFontSubpixelQuantization(_:)](coregraphics/cgcontext/setallowsfontsubpixelquantization(_:).md)
- [setShouldSmoothFonts(_:)](coregraphics/cgcontext/setshouldsmoothfonts(_:).md)
- [setShouldSubpixelPositionFonts(_:)](coregraphics/cgcontext/setshouldsubpixelpositionfonts(_:).md)
- [setShouldSubpixelQuantizeFonts(_:)](coregraphics/cgcontext/setshouldsubpixelquantizefonts(_:).md)
- [showGlyphs(g:count:)](coregraphics/cgcontext/showglyphs(g:count:).md)
- [showGlyphs(_:at:)](coregraphics/cgcontext/showglyphs(_:at:).md)
- [showGlyphsAtPoint(x:y:glyphs:count:)](coregraphics/cgcontext/showglyphsatpoint(x:y:glyphs:count:).md)
- [showGlyphsWithAdvances(glyphs:advances:count:)](coregraphics/cgcontext/showglyphswithadvances(glyphs:advances:count:).md)
- [showText(string:length:)](coregraphics/cgcontext/showtext(string:length:).md)
- [showTextAtPoint(x:y:string:length:)](coregraphics/cgcontext/showtextatpoint(x:y:string:length:).md)
- [CGTextDrawingMode](coregraphics/cgtextdrawingmode.md)

### Drawing Core Graphics Layers

- [draw(_:at:)](coregraphics/cgcontext/draw(_:at:).md)
- [draw(_:in:)](coregraphics/cgcontext/draw(_:in:).md)

### Setting Fill, Stroke, and Shadow Colors

- [setFillColor(_:)](coregraphics/cgcontext/setfillcolor(_:)-8lhn8.md)
- [setFillColor(_:)](coregraphics/cgcontext/setfillcolor(_:)-756dy.md)
- [setFillColor(cyan:magenta:yellow:black:alpha:)](coregraphics/cgcontext/setfillcolor(cyan:magenta:yellow:black:alpha:).md)
- [setFillColor(gray:alpha:)](coregraphics/cgcontext/setfillcolor(gray:alpha:).md)
- [setFillColor(red:green:blue:alpha:)](coregraphics/cgcontext/setfillcolor(red:green:blue:alpha:).md)
- [setFillColorSpace(_:)](coregraphics/cgcontext/setfillcolorspace(_:).md)
- [setShadow(offset:blur:)](coregraphics/cgcontext/setshadow(offset:blur:).md)
- [setShadow(offset:blur:color:)](coregraphics/cgcontext/setshadow(offset:blur:color:).md)
- [setStrokeColor(_:)](coregraphics/cgcontext/setstrokecolor(_:)-1sskg.md)
- [setStrokeColor(_:)](coregraphics/cgcontext/setstrokecolor(_:)-4pd8p.md)
- [setStrokeColor(cyan:magenta:yellow:black:alpha:)](coregraphics/cgcontext/setstrokecolor(cyan:magenta:yellow:black:alpha:).md)
- [setStrokeColor(gray:alpha:)](coregraphics/cgcontext/setstrokecolor(gray:alpha:).md)
- [setStrokeColor(red:green:blue:alpha:)](coregraphics/cgcontext/setstrokecolor(red:green:blue:alpha:).md)
- [setStrokeColorSpace(_:)](coregraphics/cgcontext/setstrokecolorspace(_:).md)
- [setStrokePattern(_:colorComponents:)](coregraphics/cgcontext/setstrokepattern(_:colorcomponents:).md)
- [setAlpha(_:)](coregraphics/cgcontext/setalpha(_:).md)

### Working with the Current Clipping Path

- [clip(using:)](coregraphics/cgcontext/clip(using:).md)
- [clip(to:)](coregraphics/cgcontext/clip(to:)-7cbwq.md)
- [clip(to:)](coregraphics/cgcontext/clip(to:)-2eg0.md)
- [clip(to:mask:)](coregraphics/cgcontext/clip(to:mask:).md)
- [boundingBoxOfClipPath](coregraphics/cgcontext/boundingboxofclippath.md)

### Working with Transparency Layers

- [beginTransparencyLayer(in:auxiliaryInfo:)](coregraphics/cgcontext/begintransparencylayer(in:auxiliaryinfo:).md)
- [beginTransparencyLayer(auxiliaryInfo:)](coregraphics/cgcontext/begintransparencylayer(auxiliaryinfo:).md)
- [endTransparencyLayer()](coregraphics/cgcontext/endtransparencylayer().md)

### Working with the Current Transformation Matrix

- [ctm](coregraphics/cgcontext/ctm.md)
- [rotate(by:)](coregraphics/cgcontext/rotate(by:).md)
- [scaleBy(x:y:)](coregraphics/cgcontext/scaleby(x:y:).md)
- [translateBy(x:y:)](coregraphics/cgcontext/translateby(x:y:).md)
- [concatenate(_:)](coregraphics/cgcontext/concatenate(_:).md)

### Setting Path Drawing Options

- [setAllowsAntialiasing(_:)](coregraphics/cgcontext/setallowsantialiasing(_:).md)
- [setFlatness(_:)](coregraphics/cgcontext/setflatness(_:).md)
- [setLineCap(_:)](coregraphics/cgcontext/setlinecap(_:).md)
- [setLineDash(phase:lengths:)](coregraphics/cgcontext/setlinedash(phase:lengths:).md)
- [setLineJoin(_:)](coregraphics/cgcontext/setlinejoin(_:).md)
- [setLineWidth(_:)](coregraphics/cgcontext/setlinewidth(_:).md)
- [setMiterLimit(_:)](coregraphics/cgcontext/setmiterlimit(_:).md)
- [setPatternPhase(_:)](coregraphics/cgcontext/setpatternphase(_:).md)
- [setFillPattern(_:colorComponents:)](coregraphics/cgcontext/setfillpattern(_:colorcomponents:).md)
- [setShouldAntialias(_:)](coregraphics/cgcontext/setshouldantialias(_:).md)

### Saving and Restoring Graphics State

- [saveGState()](coregraphics/cgcontext/savegstate().md)
- [restoreGState()](coregraphics/cgcontext/restoregstate().md)

### Managing a Graphics Context

- [flush()](coregraphics/cgcontext/flush().md)
- [synchronize()](coregraphics/cgcontext/synchronize().md)
- [setBlendMode(_:)](coregraphics/cgcontext/setblendmode(_:).md)
- [CGBlendMode](coregraphics/cgblendmode.md)
- [setRenderingIntent(_:)](coregraphics/cgcontext/setrenderingintent(_:).md)

### Managing a Bitmap Graphics Context

- [bitmapInfo](coregraphics/cgcontext/bitmapinfo.md)
- [alphaInfo](coregraphics/cgcontext/alphainfo.md)
- [bitsPerComponent](coregraphics/cgcontext/bitspercomponent.md)
- [bitsPerPixel](coregraphics/cgcontext/bitsperpixel.md)
- [bytesPerRow](coregraphics/cgcontext/bytesperrow.md)
- [colorSpace](coregraphics/cgcontext/colorspace.md)
- [data](coregraphics/cgcontext/data.md)
- [height](coregraphics/cgcontext/height.md)
- [width](coregraphics/cgcontext/width.md)
- [makeImage()](coregraphics/cgcontext/makeimage().md)

### Managing a PDF Graphics Context

- [beginPDFPage(_:)](coregraphics/cgcontext/beginpdfpage(_:).md)
- [endPDFPage()](coregraphics/cgcontext/endpdfpage().md)
- [addDestination(_:at:)](coregraphics/cgcontext/adddestination(_:at:).md)
- [setDestination(_:for:)](coregraphics/cgcontext/setdestination(_:for:).md)
- [setURL(_:for:)](coregraphics/cgcontext/seturl(_:for:).md)
- [addDocumentMetadata(_:)](coregraphics/cgcontext/adddocumentmetadata(_:).md)
- [closePDF()](coregraphics/cgcontext/closepdf().md)

### Managing a Page-Based Graphics Context

- [beginPage(mediaBox:)](coregraphics/cgcontext/beginpage(mediabox:).md)
- [endPage()](coregraphics/cgcontext/endpage().md)

### Working with Core Foundation Types

- [typeID](coregraphics/cgcontext/typeid.md)

### Constants

- [CGPathFillRule](coregraphics/cgpathfillrule.md)
- [CGTextEncoding](coregraphics/cgtextencoding.md)

### Instance Methods

- [draw(_:in:by:options:)](coregraphics/cgcontext/draw(_:in:by:options:).md)
- [resetClip()](coregraphics/cgcontext/resetclip().md)
- [setEDRTargetHeadroom(_:)](coregraphics/cgcontext/setedrtargetheadroom(_:).md)
- [synchronizeAttributes()](coregraphics/cgcontext/synchronizeattributes().md)

### Structures

- [CGContext.AuxiliaryInfo](coregraphics/cgcontext/auxiliaryinfo.md)

### Initializers

- [init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:)](coregraphics/cgcontext/init(data:width:height:bitspercomponent:bytesperrow:space:bitmapinfo:)-10b3i.md)
- [init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:)](coregraphics/cgcontext/init(data:width:height:bitspercomponent:bytesperrow:space:bitmapinfo:)-4fkaf.md)
- [init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:releaseCallback:releaseInfo:)](coregraphics/cgcontext/init(data:width:height:bitspercomponent:bytesperrow:space:bitmapinfo:releasecallback:releaseinfo:)-4yzt5.md)
- [init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:releaseCallback:releaseInfo:)](coregraphics/cgcontext/init(data:width:height:bitspercomponent:bytesperrow:space:bitmapinfo:releasecallback:releaseinfo:)-71ea9.md)

### Instance Properties

- [contentToneMappingInfo](coregraphics/cgcontext/contenttonemappinginfo.md)

## Relationships

### Conforms To

- [Copyable](swift/copyable.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)

## See Also

### Related Documentation

- [Quartz 2D Programming Guide](apple-archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction.md)

### 2D Drawing

- [CGImage](coregraphics/cgimage.md)
- [CGPath](coregraphics/cgpath.md)
- [CGMutablePath](coregraphics/cgmutablepath.md)
- [CGLayer](coregraphics/cglayer.md)
