<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/GraphicsContext/withCGContext(content:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI15GraphicsContextV13withCGContext7contentyySo0F3RefaKXE_tKF"
  },
  "title" : "withCGContext(content:)"
}
-->

# withCGContext(content:)

Provides a Core Graphics context that you can use as a proxy to draw
into this context.

```
func withCGContext(content: (CGContext) throws -> Void) rethrows
```

## Parameters

`content`

A closure that receives a
<doc://com.apple.documentation/documentation/CoreGraphics/CGContext>
that you use to perform drawing operations, just like you draw into a
[`GraphicsContext`](/documentation/SwiftUI/GraphicsContext) instance. Any filters, blend mode settings, clip
masks, and other state set before calling `withCGContext(content:)`
apply to drawing operations in the Core Graphics context as well. Any
state you set on the Core Graphics context is lost when the closure
returns. Accessing the Core Graphics context after the closure
returns produces undefined behavior.

## Discussion

Use this method to use existing drawing code that relies on
Core Graphics primitives.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)