MacOS AppKit文档翻译 - NSImage

标签: NSImageAppKitMacOS
2140人阅读 评论(2) 收藏 举报

NSImageView 的使用请看: http://blog.csdn.net/lovechris00/article/details/77983328


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


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

如何实现NSImage对其指定像素的保持及拉伸? 在UIKit中的UIImage有以下两个方法可以实现对图片的部分拉伸: - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NS...
  • freeWayWalker
  • freeWayWalker
  • 2014-03-16 12:23
  • 4060


转自:http://www.leoshao.com/57.html 刚开始用NSImage,从网上查找的方法是调用 imageNamed方法 NSImage *myImage = [NSImage imageNamed:@"imageName.png"] ;
  • henry_sea
  • henry_sea
  • 2011-09-28 23:24
  • 4796

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

学习Mac开发第四弹 通过NSImageView加载图片 创建NSImageView 1 . 拖拽大法好 Autolayout 创建一个宽高160 *160 的NSImageView 创建是正方形的 运行后图片居然没有进行铺满。  修改下Scaling为None...
  • lengshengren
  • lengshengren
  • 2017-01-04 18:58
  • 1807


转自:http://www.tanhao.me/pieces/1408.html/ 在UIKit中的UIImage有以下两个方法可以实现对图片的部分拉伸:- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidt...
  • qs_csu
  • qs_csu
  • 2016-12-22 17:31
  • 766

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

自己在视图View中画图片DrawImage时用到了图片数据类型的转换,觉得还不错,转过来看看 将NSImage转换为CIImage #import // convert NSImage to bitmap  NSImage * myImage  ...
  • xxq_2011
  • xxq_2011
  • 2012-07-26 09:38
  • 1998


这里例子中将NSImage对象以png的格式保存到本地硬盘。 - (void )saveImage:(NSImage *)image {     [image lockFocus];        ...
  • xxq_2011
  • xxq_2011
  • 2013-08-18 18:17
  • 2750


对NSImage的一个扩展作者:Codelif    学习了Cocoa的图片制作,受益很多。知道了图片是怎么画出来的。上层原理。可以对图片的图层操作,如添加一个图层,删除图层,图片的混合,图片的缩放,矢量图图的制作,把字符串图片化,制作pdf的一些原理等。////...
  • ydfok
  • ydfok
  • 2008-12-22 13:57
  • 5912

MacOS AppKit文档翻译 - NSImage

本文转自:http://www.jianshu.com/p/7f00e6a8862d NSImageView 的使用请看: http://blog.csdn.net/lovechris00/article/details/77983328NSImage Overview Using Images ...
  • lovechris00
  • lovechris00
  • 2017-09-15 10:52
  • 2140


- (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size { NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSRect targetFrame = NSM...
  • moqj_123
  • moqj_123
  • 2014-10-22 19:28
  • 626

[Cocoa]_[初级]_[使用NSAffineTransfrom 对NSImage图片进行旋转]

-(NSImage *)roateSourceImage:(NSImage *) sourceImage ByDegrees:(CGFloat)degrees { NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSRe...
  • moqj_123
  • moqj_123
  • 2014-11-20 17:28
  • 1179
    • 访问:210168次
    • 积分:3453
    • 等级:
    • 排名:第11457名
    • 原创:127篇
    • 转载:6篇
    • 译文:1篇
    • 评论:96条