Next Page > Hide TOC

NSImageRep Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AppKit.framework
Availability
Available in Mac OS X v10.0 and later.
Companion guide
Declared in
NSImageRep.h
Related sample code

Overview

The NSImageRep class is a semiabstract superclass (“semi” because it has some instance variables and implementation of its own). Each of its subclasses knows how to draw an image from a particular kind of source data. While an NSImageRep subclass can be used directly, it is typically through an NSImage object. An NSImage object manages a group of image representations, choosing the best one for the current output device.

Adopted Protocols

NSCoding
NSCopying

Tasks

Creating an NSImageRep

Determining the Supported Image Types

Setting the Size of the Image

Specifying Information About the Representation

Drawing the Image

Managing NSImageRep Subclasses

Class Methods

canInitWithData:

Returns a Boolean value indicating whether the receiver can initialize itself from the specified data.

+ (BOOL)canInitWithData:(NSData *)data

Parameters
data

The image data.

Return Value

YES if the receiver understands the format of the specified data and can use it to initialize itself; otherwise, NO.

Discussion

This method should be overridden by subclasses. Note that this method does not need to do a comprehensive check of the image data; it should return NO only if it knows it cannot initialize itself from the data.

Availability
See Also
Declared In
NSImageRep.h

canInitWithPasteboard:

Returns a Boolean value indicating whether the receiver can initialize itself from the data on the specified pasteboard.

+ (BOOL)canInitWithPasteboard:(NSPasteboard *)pasteboard

Parameters
pasteboard

The pasteboard containing the image data.

Return Value

YES if the receiver understands the format of the specified data and can use it to initialize itself; otherwise, NO.

Discussion

This method invokes the imageUnfilteredPasteboardTypes class method and checks the list of types returned by that method against the data types in pasteboard. If it finds a match, it returns YES. When creating a subclass of NSImageRep that accepts image data from a non-default pasteboard type, override the imageUnfilteredPasteboardTypes method to assure this method returns the correct response.

Availability
See Also
Declared In
NSImageRep.h

imageFileTypes

Returns the file types supported by NSImageRep or one of its subclasses.

+ (NSArray *)imageFileTypes

Return Value

An array of NSString objects, each of which contains a filename extension or HFS file type of a supported format.

Discussion

The list includes both those types returned by the imageUnfilteredFileTypes class method plus those that can be converted to a supported type by a user-installed filter service. The returned file types can include encoded HFS file types as well as filename extensions.

Don’t override this method when subclassing NSImageRep—it always returns a valid list for any subclass of NSImageRep that correctly overrides the imageUnfilteredFileTypes method.

Availability
Declared In
NSImageRep.h

imagePasteboardTypes

Returns the pasteboard types supported by NSImageRep or one of its subclasses.

+ (NSArray *)imagePasteboardTypes

Return Value

An array of NSString objects, each of which contains a supported pasteboard format.

Discussion

The list includes both those types returned by the imageUnfilteredPasteboardTypes class method plus those that can be converted to a supported type by a user-installed filter service. Don’t override this method when subclassing NSImageRep—it always returns a valid list for any subclass of NSImageRep that correctly overrides the imageUnfilteredPasteboardTypes method.

Availability
Declared In
NSImageRep.h

imageRepClassForData:

Returns the NSImageRep subclass that handles the specified type of data.

+ (Class)imageRepClassForData:(NSData *)data

Parameters
data

The image data.

Return Value

A Class object for the image representation that can handle the data, or nil if no image representation could handle the data.

Availability
Declared In
NSImageRep.h

imageRepClassForFileType:

Returns the NSImageRep subclass that handles data with the specified type.

+ (Class)imageRepClassForFileType:(NSString *)type

Parameters
type

A string containing the filename extension or an encoded HFS type.

Return Value

A Class object for the image representation that can handle the type of data, or nil if no image representation could handle the type.

Availability
Declared In
NSImageRep.h

imageRepClassForPasteboardType:

Returns the NSImageRep subclass that handles data with the specified pasteboard type.

+ (Class)imageRepClassForPasteboardType:(NSString *)type

Parameters
type

The pasteboard type.

Return Value

A Class object for the image representation that can handle the specified pasteboard type, or nil if no image representation could handle the type.

Availability
Declared In
NSImageRep.h

imageRepClassForType:

Returns the NSImageRep subclass that handles image data for the specified UTI.

+ (Class)imageRepClassForType:(NSString *)type

Parameters
type

The UTI string identifying the desired image type. Some sample image-related UTI strings include "public.image”, "public.jpeg”, and "public.tiff”. For a list of supported types, see UTCoreTypes.h.

Return Value

A Class object for the image representation that can handle the UTI, or nil if no image representation could handle the data.

Availability
Declared In
NSImageRep.h

imageRepsWithContentsOfFile:

Creates and returns an array of image representation objects initialized using the contents of the specified file.

+ (NSArray *)imageRepsWithContentsOfFile:(NSString *)filename

Parameters
filename

A full or relative pathname specifying the file to open. This string should include the filename extension.

Return Value

An array of image representation objects. The array contains one object for each image in the specified file.

Discussion

If sent to the NSImageRep class object, this method returns an array of objects (all newly allocated instances of a subclass of NSImageRep, chosen through the use of imageRepClassForFileType:) that have been initialized with the contents of the file. If sent to a subclass of NSImageRep that recognizes the file type, this method returns an array of objects (all instances of that subclass) that have been initialized with the contents of the file.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the contents of the file and passing it to the imageRepsWithData: method of the subclass. By default, the files handled include those with the extensions “tiff”, “gif”, “jpg”, “pict”, “pdf”, and “eps”.

Availability
See Also
Declared In
NSImageRep.h

imageRepsWithContentsOfURL:

Creates and returns an array of image representation objects initialized using the contents of the specified URL.

+ (NSArray *)imageRepsWithContentsOfURL:(NSURL *)aURL

Parameters
aURL

The URL pointing to the image data.

Return Value

An array of image representation objects. The array contains one object for each image in the data at the specified URL.

Discussion

If sent to the NSImageRep class object, this method returns an array of objects (all newly allocated instances of a subclass of NSImageRep) that have been initialized with the contents of the specified URL. If sent to a subclass of NSImageRep that recognizes the data at the specified URL, it returns an array of objects (all instances of that subclass) that have been initialized with the contents of that URL.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the contents of the specified URL and passing it to the imageRepsWithData: method.

Availability
Declared In
NSImageRep.h

imageRepsWithPasteboard:

Creates and returns an array of image representation objects initialized using the contents of the pasteboard.

+ (NSArray *)imageRepsWithPasteboard:(NSPasteboard *)pasteboard

Parameters
pasteboard

The pasteboard containing the image data.

Return Value

An array of image representation objects. The array contains one object for each image in the specified pasteboard.

Discussion

If sent to the NSImageRep class object, this method returns an array of objects (all newly-allocated instances of a subclass of NSImageRep) that have been initialized with the data in the specified pasteboard. If sent to a subclass of NSImageRep that recognizes the pasteboard data, it returns an array of objects (all instances of that subclass) initialized with the pasteboard data.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the data in pasteboard and passing it to the imageRepsWithData: method.

Availability
See Also
Declared In
NSImageRep.h

imageRepWithContentsOfFile:

Creates and returns an image representation object using the contents of the specified file.

+ (id)imageRepWithContentsOfFile:(NSString *)filename

Parameters
filename

A full or relative pathname specifying the file to open. This string should include the filename extension.

Return Value

An initialized instance of an NSImageRep subclass, or nil if the image data could not be read.

Discussion

If sent to the NSImageRep class object, this method returns a newly allocated instance of a subclass of NSImageRep (chosen through the use of imageRepClassForFileType:) initialized with the contents of the specified file. If sent to a subclass of NSImageRep that recognizes the type of data in the file, it returns an instance of that subclass initialized with the contents of the file.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the contents of the file and passing it to the imageRepWithData: method. By default, the files handled include those with the extensions “tiff”, “gif”, “jpg”, “pict”, “pdf”, and “eps”.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

imageRepWithContentsOfURL:

Creates and returns an image representation object using the data at the specified URL

+ (id)imageRepWithContentsOfURL:(NSURL *)aURL

Parameters
aURL

The URL pointing to the image data.

Return Value

An initialized instance of an NSImageRep subclass, or nil if the image data could not be read.

Discussion

If sent to the NSImageRep class object, this method returns a newly allocated instance of a subclass of NSImageRep initialized with the contents of the specified URL. If sent to a subclass of NSImageRep that recognizes the data contained in the URL, it returns an instance of that subclass initialized with the data in the URL.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the contents of the file, then passing it to the imageRepWithData: method.

Availability
Declared In
NSImageRep.h

imageRepWithPasteboard:

Creates and returns an image representation object using the contents of the specified pasteboard.

+ (id)imageRepWithPasteboard:(NSPasteboard *)pasteboard

Parameters
pasteboard

The pasteboard containing the image data.

Return Value

An initialized instance of an NSImageRep subclass, or nil if the image data could not be read.

Discussion

If sent to the NSImageRep class object, this method returns a newly allocated instance of a subclass of NSImageRep initialized with the data in the specified pasteboard. If sent to a subclass of NSImageRep that recognizes the data on the pasteboard, it returns an instance of that subclass initialized with that data.

This method returns nil in any of the following cases:

The NSImageRep subclass is initialized by creating an NSData object based on the data the specified pasteboard and passing it to the imageRepWithData: method.

Availability
See Also
Declared In
NSImageRep.h

imageTypes

Returns an array of UTI strings identifying the image types supported by the receiver, either directly or through a user-installed filter service.

+ (NSArray *)imageTypes

Return Value

An array of NSString objects, each of which contains a UTI identifying a supported image type. Some sample image-related UTI strings include "public.image”, "public.jpeg”, and "public.tiff”. For a list of supported types, see UTCoreTypes.h.

Discussion

The returned list includes UTIs all file types supported by this image representation object plus those that can be opened by this image representation after being converted by a user-installed filter service. You can use the returned UTI strings with any method that supports UTIs.

Availability
Declared In
NSImageRep.h

imageUnfilteredFileTypes

Returns the list of file types supported directly by the receiver.

+ (NSArray *)imageUnfilteredFileTypes

Return Value

An array of NSString objects. This array is empty by default. Subclasses must override to return the list of file formats they support.

Discussion

The returned file types can include encoded HFS file types as well as filename extensions. When creating a subclass of NSImageRep, override this method to return a list of strings representing the supported file types. For example, the NSBitmapImageRep class implements code similar to the following for this method:

+ (NSArray *)imageUnfilteredFileTypes {
    static NSArray *types = nil;
 
    if (!types) types = [[NSArray alloc]
        initWithObjects:@"tiff", @"gif", @"jpg",  @"bmp", nil];
    return types;
}

If your subclass supports the types supported by its superclass, you must explicitly get the array of types from the superclass and put them in the array returned by this method.

Availability
See Also
Declared In
NSImageRep.h

imageUnfilteredPasteboardTypes

Returns the list of pasteboard types supported directly by the receiver.

+ (NSArray *)imageUnfilteredPasteboardTypes

Return Value

An array of NSString objects. This array is empty by default. Subclasses must override to return the list of pasteboard formats they support.

Discussion

When creating a subclass of NSImageRep, override this method to return a list representing the supported pasteboard types. For example, the NSBitmapImageRep class implements code similar to the following for this method:

+ (NSArray *)imageUnfilteredPasteboardTypes {
    static NSArray *types = nil;
 
    if (!types) types = [[NSArray alloc] initWithObjects:NSTIFFPboardType,  nil];
    return types;
}

If your subclass supports the types supported by its superclass, you must explicitly get the list of types from the superclass and add them to the array returned by this method.

Availability
See Also
Declared In
NSImageRep.h

imageUnfilteredTypes

Returns an array of UTI strings identifying the image types supported directly by the receiver.

+ (NSArray *)imageUnfilteredTypes

Return Value

An array of NSString objects, each of which contains a UTI identifying a supported image type. Some sample image-related UTI strings include "public.image”, "public.jpeg”, and "public.tiff”. For a list of supported types, see UTCoreTypes.h.

Discussion

The returned list includes UTI strings only for those file types that are supported directly by the receiver. It does not include types that are supported through user-installed filter services. You can use the returned UTI strings with any method that supports UTIs.

Availability
Declared In
NSImageRep.h

registeredImageRepClasses

Returns an array containing the registered NSImageRep classes.

+ (NSArray *)registeredImageRepClasses

Return Value

An array of Class objects identifying the registered NSImageRep subclasses.

Availability
Declared In
NSImageRep.h

registerImageRepClass:

Adds the specified class to the registry of available NSImageRep subclasses.

+ (void)registerImageRepClass:(Class)imageRepClass

Parameters
imageRepClass

The Class object for an NSImageRep subclass.

Discussion

This method posts an NSImageRepRegistryDidChangeNotification, along with the receiving object, to the default notification center.

A good place to add image representation classes to the registry is in the load class method.

Availability
See Also
Declared In
NSImageRep.h

unregisterImageRepClass:

Removes the specified NSImageRep subclass from the registry of available image representations.

+ (void)unregisterImageRepClass:(Class)imageRepClass

Parameters
imageRepClass

The Class object for an NSImageRep subclass.

Discussion

This method posts the NSImageRepRegistryDidChangeNotification, along with the receiving object, to the default notification center.

Availability
See Also
Declared In
NSImageRep.h

Instance Methods

bitsPerSample

Returns the number of bits per sample in the receiver.

- (NSInteger)bitsPerSample

Return Value

The number of bits used to specify each component of data in a single pixel (for example, a value of 8 for an RGBA image means that each pixel is comprised of four 8-bit values). May also return NSImageRepMatchesDevice.

Discussion

If the receiver is a planar image, this method returns the number of bits per sample per plane.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

colorSpaceName

Returns the name of the receiver's color space.

- (NSString *)colorSpaceName

Return Value

The colorspace name, or NSCalibratedRGBColorSpace if no name has been assigned.

Availability
See Also
Declared In
NSImageRep.h

draw

Implemented by subclasses to draw the image in the current coordinate system.

- (BOOL)draw

Return Value

YES if the image was successfully drawn; otherwise, NO if there was a problem. The default version of this method simply returns YES.

Discussion

Subclass override this method to draw the image using the image data. By the time this method is called, the graphics state is already configured for you to draw the image at location (0.0, 0.0) in the current coordinate system.

The standard Application Kit subclasses all draw the image using the NSCompositeCopy composite operation defined in the “Constants” section of NSImage. Using the copy operator, the image data overwrites the destination without any blending effects. Transparent (alpha) regions in the source image appear black. To use other composite operations, you must place the representation into an NSImage object and use its drawAtPoint:fromRect:operation:fraction: or drawInRect:fromRect:operation:fraction: methods.

Availability
Declared In
NSImageRep.h

drawAtPoint:

Draws the receiver's image data at the specified point in the current coordinate system.

- (BOOL)drawAtPoint:(NSPoint)aPoint

Parameters
aPoint

The point in the current coordinate system at which to draw the image.

Return Value

YES if the image was successfully drawn; otherwise, NO. If the size of the image has not yet been set, this method returns NO immediately

Discussion

This method sets the origin of the current coordinate system to the specified point and then invokes the receiver’s draw method to draw the image at that point. Upon completion, it restores the current coordinates to their original setting. If aPoint is (0.0, 0.0), this method simply invokes the draw method.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

drawInRect:

Draws the image, scaling it (as needed) to fit the specified rectangle.

- (BOOL)drawInRect:(NSRect)rect

Parameters
rect

The rectangle in the current coordinate system in which to draw the image.

Return Value

YES if the image was successfully drawn; otherwise, NO. If the size of the image has not yet been set, this method returns NO immediately

Discussion

This method sets the origin of the current coordinate system to the origin of the specified rectangle before invoking the receiver's draw method. If the rectangle size is different from the image's native size, this method adjusts the coordinate transform, causing the image to be scaled appropriately. After the draw method returns, the coordinate system changes are undone, restoring the original graphics state.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

hasAlpha

Returns a Boolean value indicating whether the receiver has an alpha channel.

- (BOOL)hasAlpha

Return Value

YES if the receiver has a known alpha channel; otherwise, NO.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

isOpaque

Returns a Boolean value indicating whether the receiver is opaque.

- (BOOL)isOpaque

Return Value

YES if the receiver is opaque; otherwise, NO.

Discussion

Use this method to test whether an image representation completely covers the area within the rectangle returned by the size method.

The returned value does not indicate whether the image has an alpha channel or if there is partial or complete transparency when drawing the image rep. Use the hasAlpha method to determine if the image has an alpha channel.

Availability
See Also
Declared In
NSImageRep.h

pixelsHigh

Returns the height of the image, measured in pixels.

- (NSInteger)pixelsHigh

Return Value

The height of the image, measured in the units of the device coordinate space. This value is usually derived from the image data itself.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

pixelsWide

Returns the width of the image, measured in pixels.

- (NSInteger)pixelsWide

Return Value

The width of the image, measured in the units of the device coordinate space. This value is usually derived from the image data itself.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

setAlpha:

Informs the receiver that its image data has an alpha component.

- (void)setAlpha:(BOOL)flag

Parameters
flag

YES if you want the receiver to have an alpha component; otherwise NO.

Discussion

Subclasses should call this method when loading image data to notify the parent class whether that data contains an alpha component. Passing in a value of YES does not add an alpha channel to the image data itself; it merely records the fact that the data has an alpha channel.

Availability
See Also
Declared In
NSImageRep.h

setBitsPerSample:

Informs the receiver that its image data has the specified number of bits for each component of a pixel.

- (void)setBitsPerSample:(NSInteger)anInt

Parameters
anInt

The number of bits used by each component of a pixel, or NSImageRepMatchesDevice.

Discussion

Subclasses should call this method when loading image data to notify the parent class of how many bits each sample uses. Specifying a value that differs from the actual image data does not change the bit depth of the image.

Availability
See Also
Declared In
NSImageRep.h

setColorSpaceName:

Informs the receiver of the color space used by the image data.

- (void)setColorSpaceName:(NSString *)string

Parameters
string

The name of the color space used by the image data.

Discussion

By default, an NSImageRep object’s color space name is NSCalibratedRGBColorSpace. Color space names are defined as part of the NSColor class, in NSGraphics.h. The following are valid color space names:

Availability
See Also
Declared In
NSImageRep.h

setOpaque:

Sets whether the receiver's image is opaque.

- (void)setOpaque:(BOOL)flag

Parameters
flag

YES if the image should be treated as fully opaque; otherwise, NO to indicate the image may include some transparent regions.

Availability
See Also
Declared In
NSImageRep.h

setPixelsHigh:

Informs the receiver of the image data height.

- (void)setPixelsHigh:(NSInteger)anInt

Parameters
anInt

The height of the image, measured in pixels.

Discussion

Subclasses should call this method when loading image data to notify the parent class of the image height. You cannot use this method to change the actual number of pixels in the image.

Availability
See Also
Declared In
NSImageRep.h

setPixelsWide:

Informs the receiver of the image data width.

- (void)setPixelsWide:(NSInteger)anInt

Parameters
anInt

The width of the image, measured in pixels.

Discussion

Subclasses should call this method when loading image data to notify the parent class of the image width. You cannot use this method to change the actual number of pixels in the image.

Availability
See Also
Declared In
NSImageRep.h

setSize:

Sets the size of the image representation to the specified value.

- (void)setSize:(NSSize)aSize

Parameters
aSize

The new size of the image representation, measured in points in the user coordinate space.

Discussion

This method determines the size of the image when it’s rendered. It is not necessarily the same as the width and height of the image in pixels as specified by the image data, nor must it be equal to the size set for the NSImage object that wraps this image representation. You must set the image size before you can render it.

The size of an image representation combined with the physical dimensions of the image data determine the resolution of the image.

Availability
See Also
Declared In
NSImageRep.h

size

Returns the size of the image representation.

- (NSSize)size

Return Value

The size of the image representation, measured in points in the user coordinate space.

Discussion

This size is the size of the image representation when it’s rendered. It is not necessarily the same as the width and height of the image in pixels as specified by the image data, nor must it be equal to the size set for the NSImage object that wraps this image representation.

The size of an image representation combined with the physical dimensions of the image data determine the resolution of the image.

Availability
See Also
Related Sample Code
Declared In
NSImageRep.h

Constants

Display Device Matching

The following constant is used by NSImageRep to denote an attribute whose value changes to match the display device.

enum {
   NSImageRepMatchesDevice
};

Constants
NSImageRepMatchesDevice

Indicates that the value of certain attributes, such as the number of colors or bits per sample, will change to match the display device.

This value can be passed in (or received back) as the value of bitsPerSample, pixelsWide, and pixelsHigh.

Available in Mac OS X v10.0 and later.

Declared in NSImageRep.h.

Declared In
NSImageRep.h

Obsolete Change Notification Name

The following constant maps to the new notification and is for legacy code only.

#define NSImageRepRegistryChangedNotification NSImageRepRegistryDidChangeNotification

Constants
NSImageRepRegistryChangedNotification

An older name for the NSImageRepRegistryDidChangeNotification notification. Do not use.

Available in Mac OS X v10.0 and later.

Declared in NSImageRep.h.

Notifications

NSImageRepRegistryDidChangeNotification

Posted whenever the NSImageRep class registry changes.

The notification object is the image class that is registered or unregistered. This notification does not contain a userInfo dictionary.

Availability
Declared In
NSImageRep.h

Next Page > Hide TOC


© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.