<!--
{
  "availability" : [
    "macOS: 10.6.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSImageRep/cgImage(forProposedRect:context:hints:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSImageRep(im)CGImageForProposedRect:context:hints:"
  },
  "title" : "cgImage(forProposedRect:context:hints:)"
}
-->

# cgImage(forProposedRect:context:hints:)

Returns a Core Graphics image object that captures the drawing of the image.

```
func cgImage(forProposedRect proposedDestRect: UnsafeMutablePointer<NSRect>?, context: NSGraphicsContext?, hints: [NSImageRep.HintKey : Any]?) -> CGImage?
```

## Parameters

`proposedDestRect`

On input, the proposed destination rectangle for drawing the image. If `nil`, it defaults to the smallest pixel-integral rectangle containing `{{0,0}, self.size}`. The `proposedDestRect` is in user space in the reference context.

    On output, the     `proposedDestRect`     may have been altered. This is because a     <doc://com.apple.documentation/documentation/CoreGraphics/CGImage>     is necessarily pixel-integral, while an [`NSImage`](/documentation/AppKit/NSImage)     is not. In order to produce a     <doc://com.apple.documentation/documentation/CoreGraphics/CGImage>     for rect     `(0.5, 0.5, 4.0, 4.0)`     without distortion or double-antialiasing, we may have to produce a 5x5     `CGImage`    , and also inflate the     `proposedDestRect`    . Drawing the     `CGImage`     in the out-value     `proposedDestRect`     is the same as drawing the [`NSImage`](/documentation/AppKit/NSImage)     in the in-value of proposed rect.

`context`

A graphics context. Can be `nil`.

`hints`

An optional dictionary of hints that provide more context for selecting or generating the image. See [`NSImageRep.HintKey`](/documentation/AppKit/NSImageRep/HintKey) for a summary of the possible key-value pairs.

## Return Value

A <doc://com.apple.documentation/documentation/CoreGraphics/CGImage>. This may be an existing <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> if one is available. If not, a new <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> is created.

## Discussion

An [`NSImage`](/documentation/AppKit/NSImage) is potentially resolution independent, and may have representations that allow it to draw well in many contexts. A <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> is more like a single pixel-based representation. This method produces a snapshot of how the [`NSImage`](/documentation/AppKit/NSImage) would draw if it was asked to draw in the proposed rectangle in the graphics context.

All input parameters are optional. They provide hints for how to choose among existing <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> objects, or how to create one if there isn’t already a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> available. The parameters are only hints.

This method is intended as an override point for image representation subclasses that naturally have a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> available. For example, [`NSBitmapImageRep`](/documentation/AppKit/NSBitmapImageRep) overrides it to return the <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> that naturally backs the representation. You don’t need to override the method except possibly for performance, though. The [`NSImageRep`](/documentation/AppKit/NSImageRep)-level implementation will produce a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> by making a buffer and calling [`draw()`](/documentation/AppKit/NSImageRep/draw()). That’s likely to be the best possible implementation for reps that aren’t naturally <doc://com.apple.documentation/documentation/CoreGraphics/CGImage>-backed. The [`draw()`](/documentation/AppKit/NSImageRep/draw()) method remains the only method of [`NSImageRep`](/documentation/AppKit/NSImageRep) that a subclasser really needs to override.

---

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)