# NSImage

A high-level interface for manipulating image data.

# 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:

NSImage提供多种操作任务，如下：

• 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. Each format is managed by 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.

NSImage类可以通过各种样式来管理图片数据。图片的样式清单根据系统版本的不同而不同，但是一些基础样式不变，比如：TIFF, GIF, PNG, 还有PDF等。每一种样式都会由一个特定的图片类对象来管理，这些图片类对象的任务就是管理真实的图片数据。可以使用限定支持的图片类型里描述的方法来获取支持的图片样式清单。

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

# Using Images with CALayer Objects

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. That method creates an image that is suited for use as the contents of a layer and that is supports all of the layer’s gravity modes. By contrast, the NSImage class supports only the kCAGravityResize, kCAGravityResizeAspect, and kCAGravityResizeAspectFill 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 recommended scale factor for the specified image object and creates an object that can be assigned 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(将一个NSImage对象赋值给一个NSLayer对象):

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];

}

lovechris00

• 擅长领域：
• iOS
• macOS

#### MacOS 开发 - NSImageView

2017-09-14 17:43:53

#### 学习Mac开发第四弹 通过NSImageView加载图片

2017-01-04 18:58:25

#### 实现将NSImage保持指定像素的拉伸

2016-12-22 17:31:41

#### nsimage 转 png ，jpg的nsdata 类型

2016-05-30 13:10:59

#### NSImage加载图片

2011-09-28 23:24:01

#### NSView转换为NSimage 与 UIImage转换为UIimage 总结

2016-07-21 12:44:06

#### 【Mac OS X开发】实现NSImage对其指定像素的保持及拉伸

2014-03-16 12:23:36

#### NSImage，CFImage常规操作（转）

2008-12-22 13:57:00

#### 图片数据类型转换 NSImage、CIImage、CGImageRef

2012-07-26 09:38:00

#### 将一个NSImage实例保存成图片文件

2013-08-18 18:17:32