Class: UIImage

Inherits:
NSObject show all

Overview

A UIImage object is a high-level way to display image data. You can create images from files, from Quartz image objects, or from raw image data you receive. The UIImage class also offers several options for drawing images to the current graphics context using different blend modes and opacity values.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Instance Attribute Details

- (UIEdgeInsets) alignmentRectInsets (readonly)

The alignment metadata used to position the image during layout. (read-only) You can use the inset values as a hint for specifying the image contents more precisely. For example, if you have a 20 x 20 pixel icon that includes a glow effect, you might set the insets to 2, 16} to indicate the position of the underlying icon without the glow effect. Objects that incorporate images can use these insets to place the image properly within their content.

Returns:

- (UIEdgeInsets) capInsets (readonly)

The end-cap insets. (read-only) End caps specify the portion of an image that should not be resized when an image is stretched. This technique is used to implement buttons and other resizable image-based interface elements. When a button with end caps is resized, the resizing occurs only in the middle of the button, in the region between the end caps. The end caps themselves keep their original size and appearance.This property specifies the sizes of all four end caps. The middle (stretchable) portion consists of all the pixels that are not included in the end caps. These pixels are tiled, left-to-right, top-to-bottom to fill the remaining space.On a non-resizeable image, this property is set to UIEdgeInsetsZero; the image does not use end caps and the entire image is subject to stretching. To create a new image with a nonzero value for this property, use the resizableImageWithCapInsets: method. If your application specifies UIEdgeInsetsZero as the capInsets parameter, the entire image is tiled.

Returns:

- (CGImageRef) CGImage (readonly)

The underlying Quartz image data. (read-only) If the image data has been purged because of memory constraints, invoking this method forces that data to be loaded back into memory. Reloading the image data may incur a performance penalty. If the UIImage object was initialized using a CIImage object, the value of the property is NULL.

Returns:

  • (CGImageRef)

- (CIImage) CIImage (readonly)

The underlying Core Image data. (read-only) If the UIImage object was initialized using a CGImageRef, the value of the property is nil.

Returns:

- (NSTimeInterval) duration (readonly)

Returns the time interval used to display an animated image. (read-only) For a non-animated image, the value of this property is 0.0.

Returns:

  • (NSTimeInterval)

- (UIImageOrientation) imageOrientation (readonly)

The orientation of the receiver’s image. (read-only) Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see “UIImageOrientation.”

Returns:

- (Array) images (readonly)

For an animated image, this property holds the complete array of UIImage objects that make up the animation. (read-only) For a non-animated image, the value of this property is nil.

Returns:

- (UIImageResizingMode) resizingMode (readonly)

The resizing mode of the image. (read-only) The default value for this property is UIImageResizingModeTile. However, UIImage will implement the resizing mode the fastest way possible while still retaining the desired visual appearance. This means that if the region to be resized is a 1-pixel region and this property is set to UIImageResizingModeTile, the region will be stretched instead because the two are virtually indistinguishable for a region of that size and stretching is dramatically faster than tiling. To set the value of this property, you need to call either animatedResizableImageNamed:capInsets:resizingMode:duration: or resizableImageWithCapInsets:resizingMode: and specify the resizing mode using the resizingMode parameter. For a list of possible values for this property, see “UIImageResizingMode.”

Returns:

- (Float) scale (readonly)

The scale factor of the image. (read-only) If you load an image from a file whose name includes the @2x modifier, the scale is set to 2.0. You can also specify an explicit scale factor when initializing an image from a Core Graphics image. All other images are assumed to have a scale factor of 1.0. If you multiply the logical size of the image (stored in the size property) by the value in this property, you get the dimensions of the image in pixels.

Returns:

- (CGSize) size (readonly)

The dimensions of the image, taking orientation into account. (read-only) In iOS 4.0 and later, this value reflects the logical size of the image and is measured in points. In iOS 3.x and earlier, this value always reflects the dimensions of the image measured in pixels.

Returns:

Class Method Details

+ (UIImage) animatedImageNamed(name, duration:duration)

Creates and returns an animated image. This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.

Parameters:

  • name (String)

    The full or partial path to the file (sans suffix).

  • duration (NSTimeInterval)

    The duration of the animation.

Returns:

  • (UIImage)

    A new image object.

+ (UIImage) animatedImageWithImages(images, duration:duration)

Creates and returns an animated image from an existing set of images.

All images included in the animated image should share the same size and scale.

Parameters:

  • images (Array)

    An array of UIImage objects.

  • duration (NSTimeInterval)

    The duration of the animation.

Returns:

  • (UIImage)

    A new image object.

+ (UIImage) animatedResizableImageNamed(name, capInsets:capInsets, duration:duration)

Creates and returns an animated image with end caps. This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.

Parameters:

  • name (String)

    The full or partial path to the file (sans suffix).

  • capInsets (UIEdgeInsets)

    The values to use for the cap insets.

  • duration (NSTimeInterval)

    The duration of the animation.

Returns:

  • (UIImage)

    A new image object.

+ (UIImage) animatedResizableImageNamed(name, capInsets:capInsets, resizingMode:resizingMode, duration:duration)

Creates and returns an animated image with end caps and a specific resizing mode. This method is exactly the same as its counterpart animatedResizableImageNamed:capInsets:duration: except that the resizing mode of the new image object can be explicitly declared. Since the resizing mode of an image is UIImageResizingModeTile by default, this method should only be used in place of its counterpart to create an animated image that needs to be resized with the UIImageResizingModeStretch resizing mode.

Parameters:

  • name (String)

    The full or partial path to the file (sans suffix).

  • capInsets (UIEdgeInsets)

    The values to use for the cap insets.

  • resizingMode (UIImageResizingMode)

    The mode with which the interior of the image is resized.

  • duration (NSTimeInterval)

    The duration of the animation.

Returns:

  • (UIImage)

    A new animated image object with the specified cap insets and resizing mode.

+ (UIImage) imageNamed(name)

Returns the image object associated with the specified filename. This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of 1.0. If the screen has a scale of 2.0, this method first searches for an image file with the same filename with an @2x suffix appended to it. For example, if the file’s name is button, it first searches for button@2x. If it finds a 2x, it loads that image and sets the scale property of the returned UIImage object to 2.0. Otherwise, it loads the unmodified filename and sets the scale property to 1.0. See iOS App Programming Guide for more information on supporting images with different scale factors.

Parameters:

  • name (String)

    The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.

Returns:

  • (UIImage)

    The image object for the specified file, or nil if the method could not find the specified image.

+ (UIImage) imageWithCGImage(cgImage)

Creates and returns an image object representing the specified Quartz image. This method does not cache the image object. You can use the methods of the Core Graphics framework to create a Quartz image reference.

Parameters:

  • cgImage (CGImageRef)

    The Quartz image object.

Returns:

  • (UIImage)

    A new image object for the specified Quartz image, or nil if the method could not initialize the image from the specified image reference.

+ (UIImage) imageWithCGImage(imageRef, scale:scale, orientation:orientation)

Creates and returns an image object with the specified scale and orientation factors. This method does not cache the image object. You can use the methods of the Core Graphics framework to create a Quartz image reference.

Parameters:

  • imageRef (CGImageRef)

    The Quartz image object.

  • scale (Float)

    The scale factor to use when interpreting the image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

  • orientation (UIImageOrientation)

    The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

Returns:

  • (UIImage)

    A new image object for the specified Quartz image, or nil if the method could not initialize the image from the specified image reference.

+ (UIImage) imageWithCIImage(ciImage)

Creates and returns an image object that contains a Core Image object.

Parameters:

  • ciImage (CIImage)

    The Core Image object to encapsulate.

Returns:

  • (UIImage)

    A new image object.

+ (UIImage) imageWithCIImage(ciImage, scale:scale, orientation:orientation)

Creates and returns an image object based on a Core Image object and the specified attributes.

Parameters:

  • ciImage (CIImage)

    The Core Image object to encapsulate.

  • scale (Float)

    The scale factor to use when interpreting the image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

  • orientation (UIImageOrientation)

    The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

Returns:

  • (UIImage)

    A new image object.

+ (UIImage) imageWithContentsOfFile(path)

Creates and returns an image object by loading the image data from the file at the specified path. This method does not cache the image object.

Parameters:

  • path (String)

    The full or partial path to the file.

Returns:

  • (UIImage)

    A new image object for the specified file, or nil if the method could not initialize the image from the specified file.

+ (UIImage) imageWithData(data)

Creates and returns an image object that uses the specified image data. This method does not cache the image object.

Parameters:

  • data (NSData)

    The image data. This can be data from a file or data you create programmatically.

Returns:

  • (UIImage)

    A new image object for the specified data, or nil if the method could not initialize the image from the specified data.

+ (UIImage) imageWithData(data, scale:scale)

Creates and returns an image object that uses the specified image data and scale factor. This method does not cache the image object.

Parameters:

  • data (NSData)

    The image data. This can be data from a file or data you create programmatically.

  • scale (Float)

    The scale factor to use when interpreting the image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

Returns:

  • (UIImage)

    A new image object for the specified data, or nil if the method could not initialize the image from the specified data.

Instance Method Details

- (Object) drawAsPatternInRect(rect)

Draws a tiled Quartz pattern using the receiver’s contents as the tile pattern. This method uses a Quartz pattern to tile the image in the specified rectangle. The image is tiled with no gaps and the fill color is ignored. In the default coordinate system, the image tiles are situated down and to the right of the origin of the specified rectangle. This method respects any transforms applied to the current graphics context, however.

Parameters:

  • rect (CGRect)

    The rectangle (in the coordinate system of the graphics context) in which to draw the image.

Returns:

- (Object) drawAtPoint(point)

Draws the image at the specified point in the current context. This method draws the entire image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the specified point. This method respects any transforms applied to the current graphics context, however.This method draws the image at full opacity using the kCGBlendModeNormal blend mode.

Parameters:

  • point (CGPoint)

    The point at which to draw the top-left corner of the image.

Returns:

- (Object) drawAtPoint(point, blendMode:blendMode, alpha:alpha)

Draws the entire image at the specified point using the custom compositing options. This method draws the entire image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the specified point. This method respects any transforms applied to the current graphics context, however.

Parameters:

  • point (CGPoint)

    The point at which to draw the top-left corner of the image.

  • blendMode (CGBlendMode)

    The blend mode to use when compositing the image.

  • alpha (Float)

    The desired opacity of the image, specified as a value between 0.0 and 1.0. A value of 0.0 renders the image totally transparent while 1.0 renders it fully opaque. Values larger than 1.0 are interpreted as 1.0.

Returns:

- (Object) drawInRect(rect)

Draws the entire image in the specified rectangle, scaling it as needed to fit. This method draws the entire image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the origin of the specified rectangle. This method respects any transforms applied to the current graphics context, however.This method draws the image at full opacity using the kCGBlendModeNormal blend mode.

Parameters:

  • rect (CGRect)

    The rectangle (in the coordinate system of the graphics context) in which to draw the image.

Returns:

- (Object) drawInRect(rect, blendMode:blendMode, alpha:alpha)

Draws the entire image in the specified rectangle and using the specified compositing options. This method scales the image as needed to make it fit in the specified rectangle. This method draws the image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the origin of the specified rectangle. This method respects any transforms applied to the current graphics context, however.

Parameters:

  • rect (CGRect)

    The rectangle (in the coordinate system of the graphics context) in which to draw the image.

  • blendMode (CGBlendMode)

    The blend mode to use when compositing the image.

  • alpha (Float)

    The desired opacity of the image, specified as a value between 0.0 and 1.0. A value of 0.0 renders the image totally transparent while 1.0 renders it fully opaque. Values larger than 1.0 are interpreted as 1.0.

Returns:

- (UIImage) imageWithAlignmentRectInsets(alignmentInsets)

Returns a new version of the image that uses the specified alignment insets.

Parameters:

  • alignmentInsets (UIEdgeInsets)

    The alignment metadata to apply to the new image.

Returns:

  • (UIImage)

    A new image object.

- (Object) initWithCGImage(CGImage)

Initializes and returns the image object with the specified Quartz image reference.

Parameters:

  • CGImage (CGImageRef)

    A Quartz image reference.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (Object) initWithCGImage(imageRef, scale:scale, orientation:orientation)

Initializes and returns an image object with the specified scale and orientation factors

Parameters:

  • imageRef (CGImageRef)

    The Quartz image object.

  • scale (Float)

    The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

  • orientation (UIImageOrientation)

    The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (Object) initWithCIImage(ciImage)

Initializes and returns an image object with the specified Core Image object.

Parameters:

  • ciImage (CIImage)

    The Core Image object.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (Object) initWithCIImage(ciImage, scale:scale, orientation:orientation)

Initializes and returns an image object with the specified Core Image object and properties.

Parameters:

  • ciImage (CIImage)

    The Core Image object.

  • scale (Float)

    The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

  • orientation (UIImageOrientation)

    The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (Object) initWithContentsOfFile(path)

Initializes and returns the image object with the contents of the specified file. This method loads the image data into memory and marks it as purgeable. If the data is purged and needs to be reloaded, the image object loads that data again from the specified path.

Parameters:

  • path (String)

    The path to the file. This path should include the filename extension that identifies the type of the image data.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not find the file or initialize the image from its contents.

- (Object) initWithData(data)

Initializes and returns the image object with the specified data. The data in the data parameter must be formatted to match the file format of one of the system’s supported image types.

Parameters:

  • data (NSData)

    The data object containing the image data.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (Object) initWithData(data, scale:scale)

Initializes and returns the image object with the specified data and scale factor. The data in the data parameter must be formatted to match the file format of one of the system’s supported image types.

Parameters:

  • data (NSData)

    The data object containing the image data.

  • scale (Float)

    The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

Returns:

  • (Object)

    An initialized UIImage object, or nil if the method could not initialize the image from the specified data.

- (UIImage) resizableImageWithCapInsets(capInsets)

Creates and returns a new image object with the specified cap insets. You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched. During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.

Parameters:

  • capInsets (UIEdgeInsets)

    The values to use for the cap insets.

Returns:

  • (UIImage)

    A new image object with the specified cap insets.

- (UIImage) resizableImageWithCapInsets(capInsets, resizingMode:resizingMode)

Para This method is exactly the same as its counterpart resizableImageWithCapInsets: except that the resizing mode of the new image object can be explicitly declared. You should only call this method in place of its counterpart if you specifically want your image to be resized with the UIImageResizingModeStretch resizing mode.

Parameters:

  • capInsets (UIEdgeInsets)

    The values to use for the cap insets.

  • resizingMode (UIImageResizingMode)

    The mode with which the interior of the image is resized.

Returns:

  • (UIImage)

    A new image object with the specified cap insets and resizing mode.