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

# init(contentsOf:)

Creates and returns an image representation object using the data at the specified URL.

```
init?(contentsOf url: URL)
```

## Parameters

`url`

The URL pointing to the image data.

## Return Value

An initialized instance of an `NSImageRep` subclass, or `nil` if the image data could not be read.

## Discussion

If sent to the `NSImageRep` class object, this method returns a newly allocated instance of a subclass of `NSImageRep` initialized with the contents of the specified URL. If sent to a subclass of `NSImageRep` that recognizes the data contained in the URL, it returns an instance of that subclass initialized with the data in the URL.

This method returns `nil` in any of the following cases:

- The message is sent to the `NSImageRep` class object and there are no subclasses in the `NSImageRep` class registry that handle the data contained in the specified URL.
- The message is sent to a subclass of `NSImageRep` and that subclass cannot handle the data contained in the specified URL.
- The `NSImageRep` subclass is unable to initialize itself with the contents of the specified URL.

The `NSImageRep` subclass is initialized by creating an `NSData` object based on the contents of the file, then passing it to the `imageRepWithData:` method.

---

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)