MacOS AppKit文档翻译 - NSImage

转载 2017年09月15日 10:52:27

NSImageView 的使用请看:


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



MacOS 开发 - NSComboBox

定义 创建 不使用数据源方法 使用数据源和代理定义NSComboBox 继承自 NSTextField(所以有 backgroundColor 属性)。创建- (void)addComboBox{ ...


osx中用于多选一可用NSSegmentedControl, 如图所示:
  • yuanya
  • yuanya
  • 2014年03月19日 16:17
  • 706

JavaScript文档扫描 - 支持Windows, Linux & macOS

不同的操作系统,底层使用的扫描仪驱动是不同的。Linux上用SANE,Windows上用TWAIN,Mac上用TWAIN或者ICA。不管上层用的是什么高级语言 (Java, Python, JavaS...
  • yushulx
  • yushulx
  • 2016年09月06日 15:11
  • 697


在这一章将从Foundation Kit框架转移到Application Ki框架,来创建一个可以用于Mac的桌面程序 一、构建项目 进入Xcode,创建一个新Cocoa Application项...


1 . IBOutlet和IBAction关键字 IBOutlet:的定义没有任何作用,因此将不会对他进行编译。 IBAction:定义为void,即以IBAction声明的方法的返回类型是void(...


转自: 在UIKit中的UIImage有以下两个方法可以实现对图片的部分拉伸:- (UIImage *)stret...
  • qs_csu
  • qs_csu
  • 2016年12月22日 17:31
  • 600


The NSImage class incorporates an internal caching scheme aimed at improving your application’s ...
  • Koupoo
  • Koupoo
  • 2011年09月11日 17:42
  • 574


NSString 转换成NSData 对象 [objc] view plain copy   NSData* xmlData = [@"testdata" dataUsingEn...


Image Coordinate Systems Like views, NSImage objects use their own coordinate system to manage th...
  • Koupoo
  • Koupoo
  • 2011年09月11日 17:21
  • 677

iphone ios NSImage CIImage CGImage 区别及应用Quartz, Quartz Extreme, Quartz 2D Extreme, Core Image

先来看一段e文 翻译一下:     1)NSImage 是一个抽象类,它能代表各种图像类型,同时也能定义各种图像。通常,大家如果对图像的类型不在意的话,NSImage是很好用的。同时,他也...
  • linzhiji
  • linzhiji
  • 2011年09月01日 11:07
  • 10887
您举报文章:MacOS AppKit文档翻译 - NSImage