MacOS AppKit文档翻译 - NSImage

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

分享

本文转自:http://www.jianshu.com/p/7f00e6a8862d
NSImageView 的使用请看: http://blog.csdn.net/lovechris00/article/details/77983328

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

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.

使用NSImage的一个实例去加载本地的图片,或创建新的图片,或给视图层绘出最终效果图。这个类比较适用于图片相关的操作,但这个类和图片数据底层的东西没有一点关系。相反,它与一个或多个图片类的相关对象(NSImageRep的子类)协作共同管理并渲染图片数据。而大部分情况下,这部分可接触的界面是透明的。

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

NSImage提供多种操作任务,如下:

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

加载存储在本地磁盘的图片,或加载URL路径的图片。

  • Drawing images into a view or graphics context.

在视图层或图形上下文里绘制图片。

  • Providing the contents of a CALayer object.

提供CALayer对象包含的内容。

  • 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

使用CALayer类来渲染图片

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.

可以直接将NSImage赋值给CALayer对象的内容属性,这么做可能不会总是生成最好的效果。相反,使用image类对象,使用layerContents(forContentsScale:)方法可以获取对象,用这个对象来为layer层填充内容。这种方法创建了一张图片,这张图片可以被当作图层内容使用,也支持图层重力模式下的各种使用情况。对比之下,NSImage类只支持kCAGravityResize,kCAGravityResizeAspect和kCAGravityResizeAspectFill等模式。

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

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台