MacOS AppKit文档翻译 - NSImage

2017-09-25 08:20:11来源:CSDN作者:lovechris00人点击


NSImageView 的使用请看:

  • NSImage
  • Overview
  • Using Images with CALayer Objects
  • Listing 1将一个NSImage对象赋值给一个NSLayer对象


A high-level interface for manipulating image data.



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. 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.

想要获取更多关于如何在APP中使用图片的信息,详情请看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.

在调用layerContents(forContentsScale:)方法前,使用recommendedLayerContentsScale(_:)方法来获取系统为图片计算推荐的长宽值。Listing 1是一个典型的例子,将窗口预存的比率作为期望的比率。根据这个比率,使用相关代码为这张特定的图片获得系统推荐的比率,并创建一个可以赋值给layer层的对象。可以对图片使用这些相关的代码,为layer层调整出最适当精确的长宽比,也可依赖layer层的contentsGravity属性来调整图片的位置和大小。

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