+ (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle compatibleWithTraitCollection:(UITraitCollection *)traitCollection
name:图片的名字。bundle:设置为nil则默认为main bundle。traitCollection:设置为nil则为main screen中描述的traits。
该方法在系统的缓存中查找具有指定名字和特征集的图像并返回。如果在缓存中没有找到,该方法将定位到disk和asset catalog查找图像。如果没有合适的图像,将返回nil。
+ (UIImage *)imageNamed:(NSString *)name
如果屏幕的scale为2.0,该方法首先搜索带@2x后缀的文件名。如,name = @“button",会先搜索button@2x的文件。
创建新的图像对象
+ (UIImage *)imageWithContentsOfFile:(NSString *)path
path参数可为绝对路径或者相对路径。该方法不会缓存图像对象。
+ (UIImage *)imageWithData:(NSData *)data
+ (UIImage *)imageWithData:(NSData *)data
scale:(CGFloat)scale
如果scale = 1.0,将使得图像的大小跟基于像素的图像大小相匹配。如果scale = 2.0,图像的大小将会改变。
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage
+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
orientation指定图像的方向。
+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
scale:(CGFloat)scale
orientation:(UIImageOrientation)orientation
- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets
+ (UIImage *)animatedImageNamed:(NSString *)name
duration:(NSTimeInterval)duration
name:绝对路径或相对路径,无后缀。
+ (UIImage *)animatedImageWithImages:(NSArray<UIImage *> *)images
duration:(NSTimeInterval)duration
images:UIImage对象组成的数组,全部的图像应当具有相同的size和scale。
+ (UIImage *)animatedResizableImageNamed:(NSString *)name
capInsets:(UIEdgeInsets)capInsets
duration:(NSTimeInterval)duration
如果name = @"image",这个方法会尝试去加载文件名为image0,image1, ... , image1024的图像。全部图像应具有相同的size和scale。
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
+ (UIImage *)animatedResizableImageNamed:(NSString *)name
capInsets:(UIEdgeInsets)capInsets
resizingMode:(UIImageResizingMode)resizingMode
duration:(NSTimeInterval)duration
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
resizingMode:(UIImageResizingMode)resizingMode
图像的resizingMode 默认为UIImageResizingModeTile。当想要使用UIImageResizingModeStretch时,才调用该方法。
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
- (UIImage *)imageFlippedForRightToLeftLayoutDirection
水平翻转图像。
初始化UIImage对象:
- (instancetype)initWithContentsOfFile:(NSString *)path
path:文件的路径,应包含扩展名。
该方法将图像加载入内存并标记为purgeable。如果需要加载图像数据时但已被清除,图像对象会再次从指定的路径加载数据。
- (instancetype)initWithData:(NSData *)data
- (instancetype)initWithData:(NSData *)data
scale:(CGFloat)scale
- (instancetype)initWithCGImage:(CGImageRef)CGImage
- (instancetype)initWithCGImage:(CGImageRef)imageRef
scale:(CGFloat)scale
orientation:(UIImageOrientation)orientation
- (instancetype)initWithCIImage:(CIImage *)ciImage - (instancetype)initWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
图像对象的属性:
@property(nonatomic, readonly) UIImageOrientation imageOrientation
typedef enum {
UIImageOrientationUp,
UIImageOrientationDown , // 180 deg rotation
UIImageOrientationLeft , // 90 deg CW
UIImageOrientationRight , // 90 deg CCW
UIImageOrientationUpMirrored , // as above but image mirrored along
// other axis. horizontal flip
UIImageOrientationDownMirrored , // horizontal flip
UIImageOrientationLeftMirrored , // vertical flip
UIImageOrientationRightMirrored , // vertical flip
} UIImageOrientation;
@property(nonatomic, readonly) CGSize size
size反应了图像以point测量的逻辑大小。
@property(nonatomic, readonly) CGFloat scale
如果加载的图像文件名包括@2x,scale则为2.0。其他图像都假设其scale为1.0。
size * scale 可获得图像的像素大小。
@property(nonatomic, readonly) BOOL flipsForRightToLeftLayoutDirection
@property(nonatomic, readonly) UIImageResizingMode resizingMode
typedef enum {
UIImageResizingModeTile,
UIImageResizingModeStretch,
} UIImageResizingMode;
@property(nonatomic, readonly) CGImageRef CGImage
@property(nonatomic, readonly) CIImage *CIImage
@property(nonatomic, readonly) NSArray <UIImage *> *images
对于non-animated图像,images的值为nil。
@property(nonatomic, readonly) NSTimeInterval duration
对于non-animated图像,duration的值为0.0。
@property(nonatomic, readonly) UIEdgeInsets capInsets
@property(nonatomic, readonly) UIEdgeInsets alignmentRectInsets
@property(nonatomic, readonly) UIImageAsset *imageAsset
@property(nonatomic, readonly, copy) UITraitCollection *traitCollection
@property(nonatomic, readonly) UIImageRenderingMode renderingMode
typedef enum : NSInteger {
UIImageRenderingModeAutomatic,
UIImageRenderingModeAlwaysOriginal,
UIImageRenderingModeAlwaysTemplate,
information
} UIImageRenderingMode;
绘制图像:
- (void)drawAtPoint:(CGPoint)point
point:图像的左上角。
该方法绘制的图像完全不透明,并使用kCGBlendModeNormal 混合模式。
- (void)drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
- (void)drawInRect:(CGRect)rect
- (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
- (void)drawAsPatternInRect:(CGRect)rect