Class

NSImage

A high-level interface for manipulating image data.

Declaration

class NSImage : NSObject

Overview

You use instances of NSImage to load existing images, create new images, and draw the resulting image data into your views. Although you use this class predominantly for image-related operations, the class itself knows little about the underlying image data. Instead, it works in conjunction with one or more image representation objects (subclasses of NSImageRep) to manage and render the image data. For the most part, these interactions are transparent.

The NSImage class serves many purposes, providing support for the following tasks:

  • Loading images stored on disk or at a specified URL.

  • Drawing images into a view or graphics context.

  • Providing the contents of a CALayer object.

  • Creating new images based on a series of captured drawing commands.

  • Producing versions of the image in a different format.

The NSImage class itself is capable of managing image data in a variety of formats. The specific list of formats is dependent on the version of the operating system but includes many standard formats such as TIFF, JPEG, GIF, PNG, and PDF among others. AppKit manages each format using a specific type of image representation object, whose job is to manage the actual image data. You can get a list of supported formats using the methods described in Determining the Supported Image Types.

For more information about how to use image objects in your app, see Cocoa Drawing Guide.

Using Images with Core Animation Layers

Although you can assign an NSImage object directly to the contents property of a CALayer object, doing so may not always yield the best results. Instead of using your image object, you can use the layerContents(forContentsScale:) method to obtain an object that you can use for your layer’s contents. The image created by that method serves as the contents of a layer, which also supports all of the layer’s gravity modes. By contrast, the NSImage class supports only the resize, resizeAspect, and resizeAspectFill modes.

Before calling the layerContents(forContentsScale:) method, use the recommendedLayerContentsScale(_:) method to get the recommended scale factor for the resulting image. Listing 1 shows a typical example that uses the scale factor of a window’s backing store as the desired scale factor. From that scale factor, the code gets the scale factor for the specified image object and creates an object that you assign to the layer. You might use this code for images that fit the layer bounds precisely or for which you rely on the contentsGravity property of the layer to position or scale the image.

Listing 1

Assigning an image to a layer

static void updateLayerWithImageInWindow1(NSImage *image, CALayer *layer, NSWindow *window) {
   CGFloat desiredScaleFactor = [window backingScaleFactor];
   CGFloat actualScaleFactor = [image recommendedLayerContentsScale:desiredScaleFactor];
 
   id layerContents = [image layerContentsForContentsScale:actualScaleFactor];
 
   [layer setContents:layerContents];
   [layer setContentsScale:actualScaleFactor];
}

Topics

Creating Images by Name

init?(named: NSImage.Name)

Returns the image object associated with the specified name.

func setName(NSImage.Name?) -> Bool

Registers the receiver under the specified name.

func name() -> NSImage.Name?

Returns the name associated with the image, if any.

typealias NSImage.Name

Named images, defined by the system or you, for use in your app.

Creating a Dynamically Drawn Image

init(size: NSSize, flipped: Bool, drawingHandler: (NSRect) -> Bool)

Creates and returns an image object whose contents are drawn using the specified block.

Creating Images from Resource Files

init?(byReferencingFile: String)

Initializes and returns an image object using the specified file.

init(byReferencing: URL)

Initializes and returns an image object using the specified URL.

init?(contentsOfFile: String)

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

init?(contentsOf: URL)

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

Creating an Image from Existing Data

init?(data: Data)

Initializes and returns an image object using the provided image data.

init?(dataIgnoringOrientation: Data)

Initializes and returns an image object using the provided image data and ignoring the EXIF orientation tags.

init(cgImage: CGImage, size: NSSize)

Initializes and returns an image object using the contents of a Core Graphics image.

init?(pasteboard: NSPasteboard)

Initializes and returns an image object with data from the specified pasteboard.

init(iconRef: IconRef)

Initializes the image object with a Carbon-style icon resource.

Creating an Empty Image

init(size: NSSize)

Initializes and returns an image object with the specified dimensions.

Managing Image Loading and Drawing

var delegate: NSImageDelegate?

The image’s delegate object.

protocol NSImageDelegate

A set of optional methods that you can use to respond to drawing failures and manage incremental loads.

Setting the Image Attributes

var size: NSSize

The size of the receiver.

var isTemplate: Bool

Sets whether the image represents a template image.

isTemplate

Returns a Boolean value indicating whether the image is a template image.

Determining the Supported Image Types

class func canInit(with: NSPasteboard) -> Bool

Tests whether the receiver can create an instance of itself using pasteboard data.

class var imageTypes: [String]

Returns an array of UTI strings identifying the image types supported by the registered NSImageRep objects, either directly or through a user-installed filter service.

class var imageUnfilteredTypes: [String]

Returns an array of UTI strings identifying the image types supported directly by the registered image representation objects.

Working with Image Representations

func addRepresentation(NSImageRep)

Adds the specified image representation object to the image.

func addRepresentations([NSImageRep])

Adds an array of image representation objects to the receiver.

var representations: [NSImageRep]

An array containing all of the receiver's image representations.

func removeRepresentation(NSImageRep)

Removes and releases the specified image representation.

func bestRepresentation(for: NSRect, context: NSGraphicsContext?, hints: [NSImageRep.HintKey : Any]?) -> NSImageRep?

Returns the best representation of the image for the specified rectangle using the provided hints.

struct NSImageRep.HintKey

Constants for the keys to include in a hints dictionary when drawing the image.

enum NSImage.LayoutDirection

The layout direction for the image.

Setting the Image Representation Selection Criteria

var prefersColorMatch: Bool

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

var usesEPSOnResolutionMismatch: Bool

A Boolean value that indicates whether EPS representations are preferred when no other representations match the resolution of the device.

var matchesOnMultipleResolution: Bool

A Boolean value that indicates whether image representations whose resolution is an integral multiple of the device resolution are a match.

Drawing the Image

func draw(in: NSRect)

Draws the image in the specified rectangle.

func draw(at: NSPoint, from: NSRect, operation: NSCompositingOperation, fraction: CGFloat)

Draws all or part of the image at the specified point in the current coordinate system.

func draw(in: NSRect, from: NSRect, operation: NSCompositingOperation, fraction: CGFloat)

Draws all or part of the image in the specified rectangle in the current coordinate system.

func draw(in: NSRect, from: NSRect, operation: NSCompositingOperation, fraction: CGFloat, respectFlipped: Bool, hints: [NSImageRep.HintKey : Any]?)

Draws all or part of the image in the specified rectangle respecting the hints and the orientation of the current coordinate system.

func drawRepresentation(NSImageRep, in: NSRect) -> Bool

Draws the image using the specified image representation object.

enum NSCompositingOperation

Compositing operators, described in terms of having source and destination images, each having an opaque and transparent region.

Managing Drawing Options

var isValid: Bool

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

var backgroundColor: NSColor

The background color for the image.

enum NSImage.ResizingMode

The resizing mode for the image.

Managing the Focus

func lockFocus()

Prepares the image to receive drawing commands.

func lockFocusFlipped(Bool)

Prepares the image to receive drawing commands using the specified flipped state.

func unlockFocus()

Removes the focus from the receiver.

Working with Alignment Metadata

var alignmentRect: NSRect

A rectangle that you can use to position the image during layout.

Managing Caching Options

var cacheMode: NSImage.CacheMode

The image’s caching mode.

func recache()

Invalidates and frees offscreen caches of all image representations.

enum NSImage.CacheMode

Constants that specify the caching policy on a per-image basis.

Producing TIFF Data for the Image

var tiffRepresentation: Data?

A data object containing TIFF data for all of the image representations in the receiver.

func tiffRepresentation(using: NSBitmapImageRep.TIFFCompression, factor: Float) -> Data?

Returns a data object containing TIFF data with the specified compression settings for all of the image representations in the receiver.

Producing a Core Graphics Image

Managing Incremental Loads

func cancelIncrementalLoad()

Cancels the current download operation, if any, for an incrementally loaded image.

Hit Testing an Image

func hitTest(NSRect, withDestinationRect: NSRect, context: NSGraphicsContext?, hints: [NSImageRep.HintKey : Any]?, flipped: Bool) -> Bool

Returns whether the destination rectangle would intersect a non-transparent portion of the image.

Managing Image Accessibility

var accessibilityDescription: String?

The image’s accessibility description.

Using an Image with Core Animation

func layerContents(forContentsScale: CGFloat) -> Any

Returns an object that may be used as the contents of a layer.

func recommendedLayerContentsScale(CGFloat) -> CGFloat

Returns the recommended layer contents scale for this image.

Managing Axis Matching

var matchesOnlyOnBestFittingAxis: Bool

A Boolean value that indicates whether the image matches only on the best fitting axis.

Deprecated Symbols

class func imageFileTypes() -> [String]

Returns an array of strings identifying the image types supported by the registered image representation objects.

Deprecated
class func imageUnfilteredFileTypes() -> [String]

Returns an array of strings identifying the file types supported directly by the registered image representation objects.

Deprecated
class func imagePasteboardTypes() -> [NSPasteboard.PasteboardType]

Returns an array of strings identifying the pasteboard types supported directly by the registered image representation objects.

Deprecated
class func imageUnfilteredPasteboardTypes() -> [NSPasteboard.PasteboardType]

Returns an array of strings identifying the pasteboard types supported directly by the registered image representation objects.

Deprecated

See Also

Images

Providing Images for Different Appearances

Supply image resources that work for light and dark appearances and for high contrast environments.

protocol NSImageDelegate

A set of optional methods that you can use to respond to drawing failures and manage incremental loads.

class NSImageRep

A semiabstract superclass that provides subclasses that you use to draw an image from a particular type of source data.