NSImage
A high-level interface for manipulating image data.
Declaration
class NSImageOverview
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 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 Supported Types of Images.
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. The code listing below 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
Configuring and displaying symbol images in your UIinit(named:)init(systemSymbolName:accessibilityDescription:)init(systemSymbolName:variableValue:accessibilityDescription:)init(symbolName:variableValue:)init(symbolName:bundle:variableValue:)init(resource:)setName(_:)name()NSImage.Nameinit(imageLiteralResourceName:)
Creating Dynamically Drawn Images
Creating Images from Resource Files
Creating Images from Existing Data
Creating Empty Images
Creating Symbol Images
Getting the Symbol Image Configuration
Managing Loading and Drawing of Images
Setting Attributes of Images
Determining Supported Types of Images
Working with Representations of Images
addRepresentation(_:)addRepresentations(_:)representationsremoveRepresentation(_:)bestRepresentation(for:context:hints:)NSImageRep.HintKeyNSImage.LayoutDirection
Setting the Representation Selection Criteria for Images
Drawing Images
draw(in:)draw(at:from:operation:fraction:)draw(in:from:operation:fraction:)draw(in:from:operation:fraction:respectFlipped:hints:)drawRepresentation(_:in:)NSCompositingOperation