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

# lockFocusOnRepresentation:

Prepares the specified image representation to receive drawing commands.

```
- (void) lockFocusOnRepresentation:(NSImageRep *) imageRepresentation;
```

## Parameters

`imageRepresentation`

An image representation belonging to the receiver, or `nil` if you want the receiver to choose which image representation to use.

## Discussion

This method sets the current drawing context to the area of the offscreen window used to cache the specified image representation’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.

If `imageRepresentation` is `nil`, this method acts like the [`lockFocus()`](/documentation/AppKit/NSImage/lockFocus()) method, setting the focus to the best representation for the `NSImage` object.

A successful [`lockFocusOnRepresentation:`](/documentation/AppKit/NSImage/lockFocusOnRepresentation:) 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 [`lockFocusOnRepresentation:`](/documentation/AppKit/NSImage/lockFocusOnRepresentation:) is unable to focus on the specified image representation, it raises an `NSImageCacheException`.

### Special Considerations

This method is deprecated as it did not set up `imageRepresentation` as a drawing destination, it set the image up as a drawing destination, then drew `imageRepresentation` into it. You can replace this functionality with the following code fragment:

```objc
[image lockFocus];
[imageRepresentation drawInRect:NSMakeRect(0,0,[image size].width, [image size].height)];
 
[image unlockFocus];
```

## See Also

[`var isValid: Bool`](/documentation/AppKit/NSImage/isValid)

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



---

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)