UIImage(FWToolkit)

@interface UIImage (FWToolkit)

/// 从当前图片创建指定透明度的图片
- (nullable UIImage *)fw_imageWithAlpha:(CGFloat)alpha NS_REFINED_FOR_SWIFT;

/// 从当前图片混合颜色创建UIImage,默认kCGBlendModeDestinationIn模式,适合透明图标
- (nullable UIImage *)fw_imageWithTintColor:(UIColor *)tintColor NS_REFINED_FOR_SWIFT;

/// 从当前UIImage混合颜色创建UIImage,自定义模式
- (nullable UIImage *)fw_imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode NS_REFINED_FOR_SWIFT;

/// 缩放图片到指定大小
- (nullable UIImage *)fw_imageWithScaleSize:(CGSize)size NS_REFINED_FOR_SWIFT;

/// 缩放图片到指定大小,指定模式
- (nullable UIImage *)fw_imageWithScaleSize:(CGSize)size contentMode:(UIViewContentMode)contentMode NS_REFINED_FOR_SWIFT;

/// 按指定模式绘制图片
- (void)fw_drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode clipsToBounds:(BOOL)clipsToBounds NS_REFINED_FOR_SWIFT;

/// 裁剪指定区域图片
- (nullable UIImage *)fw_imageWithCropRect:(CGRect)rect NS_REFINED_FOR_SWIFT;

/// 指定颜色填充图片边缘
- (nullable UIImage *)fw_imageWithInsets:(UIEdgeInsets)insets color:(nullable UIColor *)color NS_REFINED_FOR_SWIFT;

/// 拉伸图片(平铺模式),指定端盖区域(不拉伸区域)
- (UIImage *)fw_imageWithCapInsets:(UIEdgeInsets)insets NS_REFINED_FOR_SWIFT;

/// 拉伸图片(指定模式),指定端盖区域(不拉伸区域)。Tile为平铺模式,Stretch为拉伸模式
- (UIImage *)fw_imageWithCapInsets:(UIEdgeInsets)insets resizingMode:(UIImageResizingMode)resizingMode NS_REFINED_FOR_SWIFT;

/// 生成圆角图片
- (nullable UIImage *)fw_imageWithCornerRadius:(CGFloat)radius NS_REFINED_FOR_SWIFT;

/// 按角度常数(0~360)转动图片,默认图片尺寸适应内容
- (nullable UIImage *)fw_imageWithRotateDegree:(CGFloat)degree NS_REFINED_FOR_SWIFT;

/// 按角度常数(0~360)转动图片,指定图片尺寸是否延伸来适应内容,否则图片尺寸不变,内容被裁剪
- (nullable UIImage *)fw_imageWithRotateDegree:(CGFloat)degree fitSize:(BOOL)fitSize NS_REFINED_FOR_SWIFT;

/// 生成mark图片
- (nullable UIImage *)fw_imageWithMaskImage:(UIImage *)maskImage NS_REFINED_FOR_SWIFT;

/// 图片合并,并制定叠加图片的起始位置
- (nullable UIImage *)fw_imageWithMergeImage:(UIImage *)image atPoint:(CGPoint)point NS_REFINED_FOR_SWIFT;

/// 图片应用CIFilter滤镜处理
- (nullable UIImage *)fw_imageWithFilter:(CIFilter *)filter NS_REFINED_FOR_SWIFT;

/// 压缩图片到指定字节,图片太大时会改为JPG格式。不保证图片大小一定小于该大小
- (nullable UIImage *)fw_compressImageWithMaxLength:(NSInteger)maxLength NS_REFINED_FOR_SWIFT;

/// 压缩图片到指定字节,图片太大时会改为JPG格式,可设置递减压缩率,默认0.1。不保证图片大小一定小于该大小
- (nullable NSData *)fw_compressDataWithMaxLength:(NSInteger)maxLength compressRatio:(CGFloat)compressRatio NS_REFINED_FOR_SWIFT;

/// 长边压缩图片尺寸,获取等比例的图片
- (nullable UIImage *)fw_compressImageWithMaxWidth:(NSInteger)maxWidth NS_REFINED_FOR_SWIFT;

/// 通过指定图片最长边,获取等比例的图片size
- (CGSize)fw_scaleSizeWithMaxWidth:(CGFloat)maxWidth NS_REFINED_FOR_SWIFT;

/// 获取原始渲染模式图片,始终显示原色,不显示tintColor。默认自动根据上下文
@property (nonatomic, readonly) UIImage *fw_originalImage NS_REFINED_FOR_SWIFT;

/// 获取模板渲染模式图片,始终显示tintColor,不显示原色。默认自动根据上下文
@property (nonatomic, readonly) UIImage *fw_templateImage NS_REFINED_FOR_SWIFT;

/// 判断图片是否有透明通道
@property (nonatomic, assign, readonly) BOOL fw_hasAlpha NS_REFINED_FOR_SWIFT;

/// 获取当前图片的像素大小,多倍图会放大到一倍
@property (nonatomic, assign, readonly) CGSize fw_pixelSize NS_REFINED_FOR_SWIFT;

/// 从视图创建UIImage,生成截图,主线程调用
+ (nullable UIImage *)fw_imageWithView:(UIView *)view NS_REFINED_FOR_SWIFT;

/// 从颜色创建UIImage,默认尺寸1x1
+ (nullable UIImage *)fw_imageWithColor:(UIColor *)color NS_REFINED_FOR_SWIFT;

/// 从颜色创建UIImage,指定尺寸
+ (nullable UIImage *)fw_imageWithColor:(UIColor *)color size:(CGSize)size NS_REFINED_FOR_SWIFT;

/// 从颜色创建UIImage,指定尺寸和圆角
+ (nullable UIImage *)fw_imageWithColor:(UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)radius NS_REFINED_FOR_SWIFT;

/// 从block创建UIImage,指定尺寸
+ (nullable UIImage *)fw_imageWithSize:(CGSize)size block:(void (NS_NOESCAPE ^)(CGContextRef context))block NS_REFINED_FOR_SWIFT;

/// 保存图片到相册,保存成功时error为nil
- (void)fw_saveImageWithCompletion:(nullable void (^)(NSError * _Nullable error))completion NS_SWIFT_NAME(__fw_saveImage(completion:)) NS_REFINED_FOR_SWIFT;

/// 保存视频到相册,保存成功时error为nil。如果视频地址为NSURL,需使用NSURL.path
+ (void)fw_saveVideo:(NSString *)videoPath withCompletion:(nullable void (^)(NSError * _Nullable error))completion NS_REFINED_FOR_SWIFT;

/// 获取灰度图
@property (nonatomic, readonly, nullable) UIImage *fw_grayImage NS_REFINED_FOR_SWIFT;

/// 获取图片的平均颜色
@property (nonatomic, readonly) UIColor *fw_averageColor NS_REFINED_FOR_SWIFT;

/// 倒影图片
- (nullable UIImage *)fw_imageWithReflectScale:(CGFloat)scale NS_REFINED_FOR_SWIFT;

/// 倒影图片
- (nullable UIImage *)fw_imageWithReflectScale:(CGFloat)scale gap:(CGFloat)gap alpha:(CGFloat)alpha NS_REFINED_FOR_SWIFT;

/// 阴影图片
- (nullable UIImage *)fw_imageWithShadowColor:(UIColor *)color offset:(CGSize)offset blur:(CGFloat)blur NS_REFINED_FOR_SWIFT;

/// 获取装饰图片
@property (nonatomic, readonly) UIImage *fw_maskImage NS_REFINED_FOR_SWIFT;

/// 高斯模糊图片,默认模糊半径为10,饱和度为1。注意CGContextDrawImage如果图片尺寸太大会导致内存不足闪退,建议先压缩再调用
- (nullable UIImage *)fw_imageWithBlurRadius:(CGFloat)blurRadius saturationDelta:(CGFloat)saturationDelta tintColor:(nullable UIColor *)tintColor maskImage:(nullable UIImage *)maskImage NS_REFINED_FOR_SWIFT;

/// 如果没有透明通道,增加透明通道
@property (nonatomic, readonly) UIImage *fw_alphaImage NS_REFINED_FOR_SWIFT;

/// 截取View所有视图,包括旋转缩放效果
+ (nullable UIImage *)fw_imageWithView:(UIView *)view limitWidth:(CGFloat)limitWidth NS_REFINED_FOR_SWIFT;

/// 获取AppIcon图片
+ (nullable UIImage *)fw_appIconImage NS_REFINED_FOR_SWIFT;

/// 获取AppIcon指定尺寸图片,名称格式:AppIcon60x60
+ (nullable UIImage *)fw_appIconImage:(CGSize)size NS_REFINED_FOR_SWIFT;

/// 从Pdf数据或者路径创建UIImage
+ (nullable UIImage *)fw_imageWithPdf:(id)path NS_REFINED_FOR_SWIFT;

/// 从Pdf数据或者路径创建指定大小UIImage
+ (nullable UIImage *)fw_imageWithPdf:(id)path size:(CGSize)size NS_REFINED_FOR_SWIFT;

/**
 创建渐变颜色UIImage,支持四个方向,默认向下Down
 
 @param size 图片大小
 @param colors 渐变颜色,CGColor数组,如:@[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor]
 @param locations 渐变位置,传NULL时均分,如:CGFloat locations[] = {0.0, 1.0};
 @param direction 渐变方向,自动计算startPoint和endPoint,支持四个方向,默认向下Down
 @return 渐变颜色UIImage
 */
+ (nullable UIImage *)fw_gradientImageWithSize:(CGSize)size
                                       colors:(NSArray *)colors
                                    locations:(nullable const CGFloat *)locations
                                    direction:(UISwipeGestureRecognizerDirection)direction NS_REFINED_FOR_SWIFT;

/**
 创建渐变颜色UIImage
 
 @param size 图片大小
 @param colors 渐变颜色,CGColor数组,如:@[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor]
 @param locations 渐变位置,传NULL时均分,如:CGFloat locations[] = {0.0, 1.0};
 @param startPoint 渐变开始点,需要根据rect计算
 @param endPoint 渐变结束点,需要根据rect计算
 @return 渐变颜色UIImage
 */
+ (nullable UIImage *)fw_gradientImageWithSize:(CGSize)size
                                       colors:(NSArray *)colors
                                    locations:(nullable const CGFloat *)locations
                                   startPoint:(CGPoint)startPoint
                                     endPoint:(CGPoint)endPoint NS_REFINED_FOR_SWIFT;

@end

Undocumented

  • 从当前图片创建指定透明度的图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithAlpha:(CGFloat)alpha;
  • 从当前图片混合颜色创建UIImage,默认kCGBlendModeDestinationIn模式,适合透明图标

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithTintColor:(nonnull UIColor *)tintColor;
  • 从当前UIImage混合颜色创建UIImage,自定义模式

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithTintColor:(nonnull UIColor *)tintColor
                                      blendMode:(CGBlendMode)blendMode;
  • 缩放图片到指定大小

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithScaleSize:(CGSize)size;
  • 缩放图片到指定大小,指定模式

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithScaleSize:(CGSize)size
                                    contentMode:(UIViewContentMode)contentMode;
  • 按指定模式绘制图片

    Declaration

    Objective-C

    - (void)fw_drawInRect:(CGRect)rect
          withContentMode:(UIViewContentMode)contentMode
            clipsToBounds:(BOOL)clipsToBounds;
  • 裁剪指定区域图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithCropRect:(CGRect)rect;
  • 指定颜色填充图片边缘

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithInsets:(UIEdgeInsets)insets
                                       color:(nullable UIColor *)color;
  • 拉伸图片(平铺模式),指定端盖区域(不拉伸区域)

    Declaration

    Objective-C

    - (nonnull UIImage *)fw_imageWithCapInsets:(UIEdgeInsets)insets;
  • 拉伸图片(指定模式),指定端盖区域(不拉伸区域)。Tile为平铺模式,Stretch为拉伸模式

    Declaration

    Objective-C

    - (nonnull UIImage *)fw_imageWithCapInsets:(UIEdgeInsets)insets
                                  resizingMode:(UIImageResizingMode)resizingMode;
  • 生成圆角图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithCornerRadius:(CGFloat)radius;
  • 按角度常数(0~360)转动图片,默认图片尺寸适应内容

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithRotateDegree:(CGFloat)degree;
  • 按角度常数(0~360)转动图片,指定图片尺寸是否延伸来适应内容,否则图片尺寸不变,内容被裁剪

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithRotateDegree:(CGFloat)degree
                                           fitSize:(BOOL)fitSize;
  • 生成mark图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithMaskImage:(nonnull UIImage *)maskImage;
  • 图片合并,并制定叠加图片的起始位置

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithMergeImage:(nonnull UIImage *)image
                                         atPoint:(CGPoint)point;
  • 图片应用CIFilter滤镜处理

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithFilter:(nonnull CIFilter *)filter;
  • 压缩图片到指定字节,图片太大时会改为JPG格式。不保证图片大小一定小于该大小

    Declaration

    Objective-C

    - (nullable UIImage *)fw_compressImageWithMaxLength:(NSInteger)maxLength;
  • 压缩图片到指定字节,图片太大时会改为JPG格式,可设置递减压缩率,默认0.1。不保证图片大小一定小于该大小

    Declaration

    Objective-C

    - (nullable NSData *)fw_compressDataWithMaxLength:(NSInteger)maxLength
                                        compressRatio:(CGFloat)compressRatio;
  • 长边压缩图片尺寸,获取等比例的图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_compressImageWithMaxWidth:(NSInteger)maxWidth;
  • 通过指定图片最长边,获取等比例的图片size

    Declaration

    Objective-C

    - (CGSize)fw_scaleSizeWithMaxWidth:(CGFloat)maxWidth;
  • 获取原始渲染模式图片,始终显示原色,不显示tintColor。默认自动根据上下文

    Declaration

    Objective-C

    @property (nonatomic, readonly) NS_REFINED_FOR_SWIFT UIImage *fw_originalImage;
  • 获取模板渲染模式图片,始终显示tintColor,不显示原色。默认自动根据上下文

    Declaration

    Objective-C

    @property (nonatomic, readonly) NS_REFINED_FOR_SWIFT UIImage *fw_templateImage;
  • 判断图片是否有透明通道

    Declaration

    Objective-C

    @property (nonatomic, readonly) BOOL fw_hasAlpha;
  • 获取当前图片的像素大小,多倍图会放大到一倍

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGSize fw_pixelSize;
  • 从视图创建UIImage,生成截图,主线程调用

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithView:(nonnull UIView *)view;
  • 从颜色创建UIImage,默认尺寸1x1

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithColor:(nonnull UIColor *)color;
  • 从颜色创建UIImage,指定尺寸

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithColor:(nonnull UIColor *)color
                                       size:(CGSize)size;
  • 从颜色创建UIImage,指定尺寸和圆角

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithColor:(nonnull UIColor *)color
                                       size:(CGSize)size
                               cornerRadius:(CGFloat)radius;
  • 从block创建UIImage,指定尺寸

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithSize:(CGSize)size
                                     block:(nonnull void (^)(CGContextRef _Nonnull))
                                               block;
  • 保存图片到相册,保存成功时error为nil

    Declaration

    Objective-C

    - (void)fw_saveImageWithCompletion:
        (nullable void (^)(NSError *_Nullable __strong))completion;
  • 保存视频到相册,保存成功时error为nil。如果视频地址为NSURL,需使用NSURL.path

    Declaration

    Objective-C

    + (void)fw_saveVideo:(nonnull NSString *)videoPath
          withCompletion:(nullable void (^)(NSError *_Nullable __strong))completion;
  • 获取灰度图

    Declaration

    Objective-C

    @property (nonatomic, readonly, nullable) UIImage *fw_grayImage;
  • 获取图片的平均颜色

    Declaration

    Objective-C

    @property (nonatomic, readonly) NS_REFINED_FOR_SWIFT UIColor *fw_averageColor;
  • 倒影图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithReflectScale:(CGFloat)scale;
  • 倒影图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithReflectScale:(CGFloat)scale
                                               gap:(CGFloat)gap
                                             alpha:(CGFloat)alpha;
  • 阴影图片

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithShadowColor:(nonnull UIColor *)color
                                           offset:(CGSize)offset
                                             blur:(CGFloat)blur;
  • 获取装饰图片

    Declaration

    Objective-C

    @property (nonatomic, readonly) NS_REFINED_FOR_SWIFT UIImage *fw_maskImage;
  • 高斯模糊图片,默认模糊半径为10,饱和度为1。注意CGContextDrawImage如果图片尺寸太大会导致内存不足闪退,建议先压缩再调用

    Declaration

    Objective-C

    - (nullable UIImage *)fw_imageWithBlurRadius:(CGFloat)blurRadius
                                 saturationDelta:(CGFloat)saturationDelta
                                       tintColor:(nullable UIColor *)tintColor
                                       maskImage:(nullable UIImage *)maskImage;
  • 如果没有透明通道,增加透明通道

    Declaration

    Objective-C

    @property (nonatomic, readonly) NS_REFINED_FOR_SWIFT UIImage *fw_alphaImage;
  • 截取View所有视图,包括旋转缩放效果

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithView:(nonnull UIView *)view
                                limitWidth:(CGFloat)limitWidth;
  • 获取AppIcon图片

    Declaration

    Objective-C

    + (nullable UIImage *)fw_appIconImage;
  • 获取AppIcon指定尺寸图片,名称格式:AppIcon60x60

    Declaration

    Objective-C

    + (nullable UIImage *)fw_appIconImage:(CGSize)size;
  • 从Pdf数据或者路径创建UIImage

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithPdf:(nonnull id)path;
  • 从Pdf数据或者路径创建指定大小UIImage

    Declaration

    Objective-C

    + (nullable UIImage *)fw_imageWithPdf:(nonnull id)path size:(CGSize)size;
  • 创建渐变颜色UIImage,支持四个方向,默认向下Down

    Declaration

    Objective-C

    + (nullable UIImage *)
        fw_gradientImageWithSize:(CGSize)size
                          colors:(nonnull NSArray *)colors
                       locations:(nullable const CGFloat *)locations
                       direction:(UISwipeGestureRecognizerDirection)direction;

    Parameters

    size

    图片大小

    colors

    渐变颜色,CGColor数组,如:@[(bridge id)[UIColor redColor].CGColor, (bridge id)[UIColor blueColor].CGColor]

    locations

    渐变位置,传NULL时均分,如:CGFloat locations[] = {0.0, 1.0};

    direction

    渐变方向,自动计算startPoint和endPoint,支持四个方向,默认向下Down

    Return Value

    渐变颜色UIImage

  • 创建渐变颜色UIImage

    Declaration

    Objective-C

    + (nullable UIImage *)fw_gradientImageWithSize:(CGSize)size
                                            colors:(nonnull NSArray *)colors
                                         locations:
                                             (nullable const CGFloat *)locations
                                        startPoint:(CGPoint)startPoint
                                          endPoint:(CGPoint)endPoint;

    Parameters

    size

    图片大小

    colors

    渐变颜色,CGColor数组,如:@[(bridge id)[UIColor redColor].CGColor, (bridge id)[UIColor blueColor].CGColor]

    locations

    渐变位置,传NULL时均分,如:CGFloat locations[] = {0.0, 1.0};

    startPoint

    渐变开始点,需要根据rect计算

    endPoint

    渐变结束点,需要根据rect计算

    Return Value

    渐变颜色UIImage