原创 2016年08月29日 14:31:36


// animated images. When set as UIImageView.image, animation will play in an infinite loop until removed. Drawing will render the first image

+ (nullable UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0);  // read sequence of files with suffix starting at 0 or 1
+ (nullable UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0); // sequence of files
+ (nullable UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(6_0);
+ (nullable UIImage *)animatedImageWithImages:(NSArray<UIImage *> *)images duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0);

@property(nullable, nonatomic,readonly) NSArray<UIImage *> *images   NS_AVAILABLE_IOS(5_0); // default is nil for non-animated images
@property(nonatomic,readonly) NSTimeInterval duration NS_AVAILABLE_IOS(5_0); // total duration for all frames. default is 0 for non-animated images
  • name只是一个base name,UIImage会添加0~1024后缀拼成完整文件名从bundle中load
  • UIImage中的所有UIImage共享同一个size和scale,取images中第一张图片的size和scale
  • images中所有UIImage使用同一capInsets和resizingMode

right-to-left layout

- (UIImage *)imageFlippedForRightToLeftLayoutDirection NS_AVAILABLE_IOS(9_0);
@property (nonatomic, readonly) BOOL flipsForRightToLeftLayoutDirection NS_AVAILABLE_IOS(9_0);
  • 只是设置flipsForRightToLeftLayoutDirection属性值为YES,返回的还是原图像
  • 只有当flipsForRightToLeftLayoutDirection为YES而且环境为right-to-left时(比如国家语言环境为阿拉伯,阿拉伯阅读习惯从右向左),UIImage才会以right-to-left layout显示(相当于水平反转)

render mode

// Create a version of this image with the specified rendering mode. By default, images have a rendering mode of UIImageRenderingModeAutomatic.
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
@property(nonatomic, readonly) UIImageRenderingMode renderingMode NS_AVAILABLE_IOS(7_0);
/* Images are created with UIImageRenderingModeAutomatic by default. An image with this mode is interpreted as a template image or an original image based on the context in which it is rendered. For example, navigation bars, tab bars, toolbars, and segmented controls automatically treat their foreground images as templates, while image views and web views treat their images as originals. You can use UIImageRenderingModeAlwaysTemplate to force your image to always be rendered as a template or UIImageRenderingModeAlwaysOriginal to force your image to always be rendered as an original.
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used
    UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information


// Support for constraint-based layout (auto layout)
// The alignmentRectInsets of a UIImage are used by UIImageView and other UIView and UIControl
//  subclasses that take custom images to determine the view's alignment rect insets for
//  constraint-based layout.
// The default alignmentRectInsets are UIEdgeInsetsZero.
- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets NS_AVAILABLE_IOS(6_0);
@property(nonatomic,readonly) UIEdgeInsets alignmentRectInsets NS_AVAILABLE_IOS(6_0);


- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode

@property(nonatomic,readonly) UIEdgeInsets capInsets               NS_AVAILABLE_IOS(5_0);   // default is UIEdgeInsetsZero for non resizable images
@property(nonatomic,readonly) UIImageResizingMode resizingMode NS_AVAILABLE_IOS(6_0); // default is UIImageResizingModeTile
/* UIImage will implement the resizing mode the fastest way possible while
 retaining the desired visual appearance.
 Note that if an image's resizable area is one point then UIImageResizingModeTile
 is visually indistinguishable from UIImageResizingModeStretch.
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
  • 对单一UIImage应用capInsets和resizingMode

left cap&top cap

// use resizableImageWithCapInsets: and capInsets.

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED;
@property(nonatomic,readonly) NSInteger leftCapWidth __TVOS_PROHIBITED;   // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
@property(nonatomic,readonly) NSInteger topCapHeight __TVOS_PROHIBITED;   // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1
  • 如果leftCapWidth为0,整个宽度拉升,如果leftCapWidth不为0,一个像素宽度拉伸,topCapHeight类似


iOS 中播放gif文件可以简单使用UIImage的这个方法:[UIImage animatedImageWithImages:images duration:duration]...
  • hherima
  • hherima
  • 2016年02月23日 11:08
  • 3883


我们都知道如果要从data中或者file中读取数据并包装成UIImage可以使用+ imageWithData: 和+ imageWithContentsOfFile: 但如果想把UIImage的图片...
  • wherejaly
  • wherejaly
  • 2010年04月28日 11:54
  • 22215


UIImage读取图像资源的几种方式。1、UIImage * myImage = [UIImage imageNamed:@"icon.png"];     事实上,imageNamed这种加载图片资...
  • gdmmhym
  • gdmmhym
  • 2011年07月19日 13:07
  • 6035

UIImage简谈:[UIImage imageNamed:]与 [UIImage imageWithContentsOfFile]的区别

[UIImage imageNamed:]只适合与UI界面中小的贴图的读取,而一些比较大的资源文件应该尽量避免使用这个接口。 直接读取文件路径[UIImage imageWithContentsOf...
  • u010024875
  • u010024875
  • 2015年11月02日 14:29
  • 3153

UIImageview and UIImage 之Swift学习

// //  ViewController.swift //  UIImageview and UIImage // //  Created by Mac on 15/4/12...
  • mjbaishiyun
  • mjbaishiyun
  • 2015年04月12日 14:30
  • 2318

怎么将一个UIImage 缩放变换到指定Size的UIImage

怎么将一个UIImage 缩放变换到指定Size的UIImage
  • tangaowen
  • tangaowen
  • 2011年05月28日 23:08
  • 13317


图片缩放到指定大小尺寸 - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{  // 创建一个bitmap的context  // 并把...
  • tieshuxianrezhang
  • tieshuxianrezhang
  • 2016年07月18日 20:45
  • 914


-(UIImage *)imageFromColor:(UIColor*)color { CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphic...
  • u014084081
  • u014084081
  • 2014年12月01日 16:10
  • 1196


NSURL *url = [NSURL URLWithString: @"http://www.baidu.com/img/baidu_sylogo1.gif"];// 获取的图片地址 UIImag...
  • liulushi_1988
  • liulushi_1988
  • 2013年01月05日 14:12
  • 2553


不多说了,直接贴代码吧 - (void)viewDidLoad {     [super viewDidLoad];          UIImage *image = [UI...
  • u012890196
  • u012890196
  • 2014年12月24日 14:14
  • 6020