<!--
{
  "availability" : [
    "macOS: 10.0.0 - 27.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSImage/lockFocus()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSImage(im)lockFocus"
  },
  "title" : "lockFocus()"
}
-->

# lockFocus()

Prepares the image to receive drawing commands.

```
func lockFocus()
```

## Discussion

This method sets the current drawing context to the area of the offscreen window used to cache the receiver’s contents. Subsequent drawing commands are composited to this offscreen window. If the offscreen drawing area already has some content, any new drawing commands are composited with that content. This method does not modify the original image data directly.

When locking focus, this method chooses the best image representation object available and locks focus on that object. If the receiver has no image representations, this method creates one with the default depth and locks focus on it.

A successful [`lockFocus()`](/documentation/AppKit/NSImage/lockFocus()) message must be balanced with a matching [`unlockFocus()`](/documentation/AppKit/NSImage/unlockFocus()) message to the same `NSImage` object. These messages bracket the code that draws the image.

If [`lockFocus()`](/documentation/AppKit/NSImage/lockFocus()) is unable to focus on the image, it raises an `NSImageCacheException`.

## See Also

[`valid`](/documentation/AppKit/NSImage/isValid)

A Boolean value that indicates whether it is possible to draw an image representation.

[`representations`](/documentation/AppKit/NSImage/representations)

An array containing all of the image object’s image representations.

[`prefersColorMatch`](/documentation/AppKit/NSImage/prefersColorMatch)

A Boolean value that indicates whether the image prefers to choose image representations using color-matching or resolution-matching.

[`-  bestRepresentationForDevice:`](/documentation/AppKit/NSImage/bestRepresentationForDevice:)

Returns the best representation for the device with the specified characteristics.



---

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)