You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
17 KiB

/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2020 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// UIImage+QMUI.h
// qmui
//
// Created by QMUI Team on 15/7/20.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define CGContextInspectSize(size) [QMUIHelper inspectContextSize:size]
#ifdef DEBUG
#define CGContextInspectContext(context) [QMUIHelper inspectContextIfInvalidatedInDebugMode:context]
#else
#define CGContextInspectContext(context) if(![QMUIHelper inspectContextIfInvalidatedInReleaseMode:context]){return nil;}
#endif
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, QMUIImageShape) {
QMUIImageShapeOval, // 椭圆
QMUIImageShapeTriangle, // 三角形
QMUIImageShapeDisclosureIndicator, // 列表 cell 右边的箭头
QMUIImageShapeCheckmark, // 列表 cell 右边的checkmark
QMUIImageShapeDetailButtonImage, // 列表 cell 右边的 i 按钮图片
QMUIImageShapeNavBack, // 返回按钮的箭头
QMUIImageShapeNavClose // 导航栏的关闭icon
};
typedef NS_OPTIONS(NSInteger, QMUIImageBorderPosition) {
QMUIImageBorderPositionAll = 0,
QMUIImageBorderPositionTop = 1 << 0,
QMUIImageBorderPositionLeft = 1 << 1,
QMUIImageBorderPositionBottom = 1 << 2,
QMUIImageBorderPositionRight = 1 << 3,
};
typedef NS_ENUM(NSInteger, QMUIImageResizingMode) {
QMUIImageResizingModeScaleToFill = 0, // 将图片缩放到给定的大小,不考虑宽高比例
QMUIImageResizingModeScaleAspectFit = 10, // 默认的缩放方式,将图片保持宽高比例不变的情况下缩放到不超过给定的大小(但缩放后的大小不一定与给定大小相等),不会产生空白也不会产生裁剪
QMUIImageResizingModeScaleAspectFill = 20, // 将图片保持宽高比例不变的情况下缩放到不超过给定的大小(但缩放后的大小不一定与给定大小相等),若有内容超出则会被裁剪。若裁剪则上下居中裁剪。
QMUIImageResizingModeScaleAspectFillTop, // 将图片保持宽高比例不变的情况下缩放到不超过给定的大小(但缩放后的大小不一定与给定大小相等),若有内容超出则会被裁剪。若裁剪则水平居中、垂直居上裁剪。
QMUIImageResizingModeScaleAspectFillBottom // 将图片保持宽高比例不变的情况下缩放到不超过给定的大小(但缩放后的大小不一定与给定大小相等),若有内容超出则会被裁剪。若裁剪则水平居中、垂直居下裁剪。
};
@interface UIImage (QMUI)
/**
UIImage
@param size size 0
@param opaque YES NO
@param scale 0
@param actionBlock image
@return
*/
+ (nullable UIImage *)qmui_imageWithSize:(CGSize)size opaque:(BOOL)opaque scale:(CGFloat)scale actions:(void (^)(CGContextRef contextRef))actionBlock;
/// 当前图片是否是可拉伸/平铺的,也即通过 resizableImageWithCapInsets: 处理过的图片
@property(nonatomic, assign, readonly) BOOL qmui_resizable;
/// 获取当前图片的像素大小,如果是多倍图,会被放大到一倍来算
@property(nonatomic, assign, readonly) CGSize qmui_sizeInPixel;
/**
* alpha alpha alpha alpha 1
*/
- (BOOL)qmui_opaque;
/**
* 1px*1px的矩形内
* @link http://www.bobbygeorgescu.com/2011/08/finding-average-color-of-uiimage/ @/link
*
* @return UIColor对象
*/
- (UIColor *)qmui_averageColor;
/**
*
*
* @return
*/
- (nullable UIImage *)qmui_grayImage;
/**
*
*
* @param alpha
*
* @return
*/
- (nullable UIImage *)qmui_imageWithAlpha:(CGFloat)alpha;
/**
* 使
*
* @param tintColor
*
* @return tintColor相同的新图片
*/
- (nullable UIImage *)qmui_imageWithTintColor:(nullable UIColor *)tintColor;
/**
* CIColorBlendMode
*
* @param blendColor
*
* @return
*
* @warning 线使
*/
- (nullable UIImage *)qmui_imageWithBlendColor:(nullable UIColor *)blendColor;
/**
*
*
*
*
* @param image
* @param point
*
* @return
*/
- (nullable UIImage *)qmui_imageWithImageAbove:(UIImage *)image atPoint:(CGPoint)point;
/**
* NSAttributedString里的图片与文字的间距
* @param extension
* @return
*/
- (nullable UIImage *)qmui_imageWithSpacingExtensionInsets:(UIEdgeInsets)extension;
/**
*
*
* @param rect rect
*
* @return
*/
- (nullable UIImage *)qmui_imageWithClippedRect:(CGRect)rect;
/**
*
*
* @param cornerRadius
*
* @return
*/
- (nullable UIImage *)qmui_imageWithClippedCornerRadius:(CGFloat)cornerRadius;
/**
* scale
*/
- (nullable UIImage *)qmui_imageWithClippedCornerRadius:(CGFloat)cornerRadius scale:(CGFloat)scale;
/**
* QMUIImageResizingModeScaleAspectFit 使
* @param size size size resizingMode size
*
* @return
* @see qmui_imageResizedInLimitedSize:resizingMode:scale:
*/
- (nullable UIImage *)qmui_imageResizedInLimitedSize:(CGSize)size;
/**
* QMUIImageResizingMode 使
* @param size size size resizingMode size
* @param resizingMode 使
*
* @return
* @see qmui_imageResizedInLimitedSize:resizingMode:scale:
*/
- (nullable UIImage *)qmui_imageResizedInLimitedSize:(CGSize)size resizingMode:(QMUIImageResizingMode)resizingMode;
/**
* QMUIImageResizingMode 使
* @param size size size resizingMode size
* @param resizingMode 使
* @param scale
*
* @return
*/
- (nullable UIImage *)qmui_imageResizedInLimitedSize:(CGSize)size resizingMode:(QMUIImageResizingMode)resizingMode scale:(CGFloat)scale;
/**
*
*
* @param direction
*
* @return
*/
- (nullable UIImage *)qmui_imageWithOrientation:(UIImageOrientation)direction;
/**
* borderborder的路径为path
*
* @param borderColor border的颜色
* @param path border的路径
*
* @return border的UIImage
* @warning `path.lineWidth``path.lineWidth / 2.0`
*/
- (nullable UIImage *)qmui_imageWithBorderColor:(nullable UIColor *)borderColor path:(nullable UIBezierPath *)path;
/**
* borderborder的路径为borderColorcornerRadius和borderWidth所创建的path
*
* @param borderColor border的颜色
* @param borderWidth border的宽度
* @param cornerRadius border的圆角
*
* @param dashedLengths CGFloat的数组`CGFloat dashedLengths[] = {2, 4}`线0
*
* @return border的UIImage
*/
- (nullable UIImage *)qmui_imageWithBorderColor:(nullable UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius dashedLengths:(nullable const CGFloat *)dashedLengths;
- (nullable UIImage *)qmui_imageWithBorderColor:(nullable UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius;
/**
* borderborder
*
* @param borderColor border的颜色
* @param borderWidth border的宽度
* @param borderPosition border的位置
*
* @return border的UIImage
*/
- (nullable UIImage *)qmui_imageWithBorderColor:(nullable UIColor *)borderColor borderWidth:(CGFloat)borderWidth borderPosition:(QMUIImageBorderPosition)borderPosition;
/**
* mask的图片
*
* @param maskImage mask图片
* @param usingMaskImageMode 使mask image YES NO maskImage
*
* @return mask的图片
*/
- (nullable UIImage *)qmui_imageWithMaskImage:(UIImage *)maskImage usingMaskImageMode:(BOOL)usingMaskImageMode;
/**
data animated UIImage animated UIImageimage 1 [UIImage imageWithData:]
@param data data
@return UIImage
*/
+ (nullable UIImage *)qmui_animatedImageWithData:(NSData *)data;
/**
data animated UIImage animated UIImage
@param data data
@param scale 0
@return UIImage
@see http://www.jianshu.com/p/767af9c690a3
@see https://github.com/rs/SDWebImage
*/
+ (nullable UIImage *)qmui_animatedImageWithData:(NSData *)data scale:(CGFloat)scale;
/**
mainBundle scale 1 [UIImage imageWithData:] 使 -qmui_animatedImageNamed:scale:
@param name gifgif nil
@return UIImage
*/
+ (nullable UIImage *)qmui_animatedImageNamed:(NSString *)name;
/**
mainBundle
@param name gifgif nil
@param scale 0
@return UIImage
*/
+ (nullable UIImage *)qmui_animatedImageNamed:(NSString *)name scale:(CGFloat)scale;
/**
* size为(4, 4)UIImage
*
* @param color
*
* @return UIImage
*/
+ (nullable UIImage *)qmui_imageWithColor:(nullable UIColor *)color;
/**
* UIImage
*
* @param color
* @param size
* @param cornerRadius
*
* @return UIImage
*/
+ (nullable UIImage *)qmui_imageWithColor:(nullable UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius;
/**
* UIImage
* @param color
* @param size
* @param cornerRadius 4[]
*/
+ (nullable UIImage *)qmui_imageWithColor:(nullable UIColor *)color size:(CGSize)size cornerRadiusArray:(nullable NSArray<NSNumber *> *)cornerRadius;
/**
* border的路径为path
*
* @param strokeColor border的颜色
* @param path border的路径
* @param addClip path的addClip
*
* @return border的UIImage
*/
+ (nullable UIImage *)qmui_imageWithStrokeColor:(nullable UIColor *)strokeColor size:(CGSize)size path:(nullable UIBezierPath *)path addClip:(BOOL)addClip;
/**
* border的路径为strokeColorcornerRadius和lineWidth所创建的path
*
* @param strokeColor border的颜色
* @param lineWidth border的宽度
* @param cornerRadius border的圆角
*
* @return border的UIImage
*/
+ (nullable UIImage *)qmui_imageWithStrokeColor:(nullable UIColor *)strokeColor size:(CGSize)size lineWidth:(CGFloat)lineWidth cornerRadius:(CGFloat)cornerRadius;
/**
* border
*
* @param strokeColor
* @param size
* @param lineWidth
* @param borderPosition
*
* @return UIImage
*/
+ (nullable UIImage *)qmui_imageWithStrokeColor:(nullable UIColor *)strokeColor size:(CGSize)size lineWidth:(CGFloat)lineWidth borderPosition:(QMUIImageBorderPosition)borderPosition;
/**
*
* @param shape
* @param size
* @param tintColor
*/
+ (nullable UIImage *)qmui_imageWithShape:(QMUIImageShape)shape size:(CGSize)size tintColor:(nullable UIColor *)tintColor;
/**
*
* @param shape
* @param size
* @param lineWidth size
* @param tintColor
*/
+ (nullable UIImage *)qmui_imageWithShape:(QMUIImageShape)shape size:(CGSize)size lineWidth:(CGFloat)lineWidth tintColor:(nullable UIColor *)tintColor;
/**
*
*/
+ (nullable UIImage *)qmui_imageWithAttributedString:(NSAttributedString *)attributedString;
/**
`UIView` `UIImage` 使 view.layer
@param view `UIView`
@return `UIView`
@warning UIView transform
*/
+ (nullable UIImage *)qmui_imageWithView:(UIView *)view;
/**
`UIView` `UIImage` 使 iOS 7
@param view `UIView`
@param afterUpdates
@return `UIView`
@warning UIView transform
*/
+ (nullable UIImage *)qmui_imageWithView:(UIView *)view afterScreenUpdates:(BOOL)afterUpdates;
@end
NS_ASSUME_NONNULL_END