Skip to content

Commit

Permalink
uiimage+qmui
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoon committed Dec 21, 2016
1 parent 89ec8e3 commit 0bba3df
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

qmui.xcodeproj/project.xcworkspace/xcuserdata/

## Build generated
build/
DerivedData/
Expand Down
14 changes: 7 additions & 7 deletions QMUIKit/UIKitExtensions/UIImage+QMUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#define CGContextInspectSize(size) [QMUIHelper inspectContextSize:size]

#ifdef DEBUG
#define CGContextInspectContext(context) [QMUIHelper inspectContextIfInvalidatedInDebugMode:context]
#define CGContextInspectContext(context) [QMUIHelper inspectContextIfInvalidatedInDebugMode:context]
#else
#define CGContextInspectContext(context) if(![QMUIHelper inspectContextIfInvalidatedInReleaseMode:context]){return nil;}
#define CGContextInspectContext(context) if(![QMUIHelper inspectContextIfInvalidatedInReleaseMode:context]){return nil;}
#endif

typedef NS_ENUM(NSInteger, QMUIImageShape) {
Expand Down Expand Up @@ -157,7 +157,7 @@ typedef NS_OPTIONS(NSInteger, QMUIImageBorderPosition) {
* 返回一个被mask的图片
*
* @param maskImage mask图片
* @param usingMaskImageMode 是否使用“mask image”的方式
* @param usingMaskImageMode 是否使用“mask image”的方式,若为 YES,则黑色部分显示,白色部分消失,透明部分显示,其他颜色会按照颜色的灰色度对图片做透明处理。若为 NO,则 maskImage 要求必须为灰度颜色空间的图片(黑白图),白色部分显示,黑色部分消失,透明部分消失,其他灰色度对图片做透明处理。
*
* @return 被mask的图片
*/
Expand Down Expand Up @@ -248,19 +248,19 @@ typedef NS_OPTIONS(NSInteger, QMUIImageBorderPosition) {

/**
对传进来的 `UIView` 截图,生成一个 `UIImage` 并返回
@param view 要截图的 `UIView`
@return `UIView` 的截图
*/
+ (UIImage *)imageWithView:(UIView *)view;

/**
对传进来的 `UIView` 截图,生成一个 `UIImage` 并返回
@param view 要截图的 `UIView`
@param afterUpdates 是否要在界面更新完成后才截图
@return `UIView` 的截图
*/
+ (UIImage *)imageWithView:(UIView *)view afterScreenUpdates:(BOOL)afterUpdates;
Expand Down
9 changes: 5 additions & 4 deletions QMUIKit/UIKitExtensions/UIImage+QMUI.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ - (UIColor *)averageColor {
}

- (UIImage *)grayImage {
int width = self.size.width;
int height = self.size.height;
// CGBitmapContextCreate 是无倍数的,所以要自己换算成1倍
NSInteger width = self.size.width * self.scale;
NSInteger height = self.size.height * self.scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, kCGBitmapByteOrderDefault);
CGContextInspectContext(context);
Expand All @@ -66,7 +67,7 @@ - (UIImage *)grayImage {
}
CGContextDrawImage(context,CGRectMake(0, 0, width, height), self.CGImage);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *grayImage = [UIImage imageWithCGImage:imageRef];
UIImage *grayImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
CGContextRelease(context);
return grayImage;
Expand Down Expand Up @@ -323,7 +324,7 @@ - (UIImage *)imageWithMaskImage:(UIImage *)maskImage usingMaskImageMode:(BOOL)us
} else {
// 用一个纯CGImage作为mask。这个image必须是单色(例如:黑白色、灰色)、没有alpha通道、不能被其他图片mask。系统的文档:If `mask' is an image, then it must be in a monochrome color space (e.g. DeviceGray, GenericGray, etc...), may not have alpha, and may not itself be masked by an image mask or a masking color.
// 白色部分显示,黑色部分消失,透明部分消失,其他灰色度对图片做透明处理。
mask = maskRef;
mask = maskRef;
}
CGImageRef maskedImage = CGImageCreateWithMask(self.CGImage, mask);
UIImage *returnImage = [UIImage imageWithCGImage:maskedImage scale:self.scale orientation:self.imageOrientation];
Expand Down
Binary file not shown.

0 comments on commit 0bba3df

Please sign in to comment.