<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSImage/init(byReferencing:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSImage(im)initByReferencingURL:"
  },
  "title" : "init(byReferencing:)"
}
-->

# init(byReferencing:)

Initializes and returns an image object using the specified URL.

```
convenience init(byReferencing url: URL)
```

## Parameters

`url`

The URL identifying the image.

## Return Value

An initialized `NSImage` object.

## Discussion

This method initializes the image object lazily. It does not attempt to retrieve the data from the specified URL or create any image representations from that data until an app attempts to draw the image or request information about it.

The `url` parameter should include a file extension that identifies the type of the image data. The mechanism that actually creates the image representation looks for an [`NSImageRep`](/documentation/AppKit/NSImageRep) subclass that handles that data type from among those registered with `NSImage`.

Because this method doesn’t actually create image representations for the image data, your app should do error checking before attempting to use the image; one way to do so is by accessing the [`isValid`](/documentation/AppKit/NSImage/isValid) property to check whether the image can be drawn.

This method invokes [`setDataRetained:`](/documentation/AppKit/NSImage/setDataRetained:) with an argument of <doc://com.apple.documentation/documentation/Swift/true>, thus enabling it to hold onto its URL. When archiving an image created with this method, only the image’s URL is written to the archive.

---

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)