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

# init(contentsOfFile:)

Initializes and returns an image object with the contents of the specified file.

```
convenience init?(contentsOfFile fileName: String)
```

## Parameters

`fileName`

A full or relative path name specifying the file with the desired image data. Relative paths must be relative to the current working directory.

## Return Value

An initialized `NSImage` object or `nil` if the method cannot create an image representation from the contents of the specified file.

## Discussion

Unlike [`init(byReferencingFile:)`](/documentation/AppKit/NSImage/init(byReferencingFile:)), which initializes an `NSImage` object lazily, this method immediately opens the specified file and creates one or more image representations from its data.

The `filename` parameter should include the file extension that identifies the type of the image data. This method looks for an `NSImageRep` subclass that handles that data type from among those registered with `NSImage`.

---

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)