Mac OS AppKit文檔翻譯——NSImage

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

}


相關鏈接:

kCAGravityResizeAspect

layerContents(forContentsScale:)?


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容