酷店

UIImageView+CornerRadius.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // UIImageView+CornerRadius.m
  3. // MyPractise
  4. //
  5. // Created by lzy on 16/3/1.
  6. // Copyright © 2016年 lzy. All rights reserved.
  7. //
  8. #import "UIImageView+CornerRadius.h"
  9. #import <objc/runtime.h>
  10. const char kProcessedImage;
  11. @interface UIImageView ()
  12. @property (assign, nonatomic) CGFloat zyRadius;
  13. @property (assign, nonatomic) UIRectCorner roundingCorners;
  14. @property (assign, nonatomic) CGFloat zyBorderWidth;
  15. @property (strong, nonatomic) UIColor *zyBorderColor;
  16. @property (assign, nonatomic) BOOL zyHadAddObserver;
  17. @property (assign, nonatomic) BOOL zyIsRounding;
  18. @end
  19. @implementation UIImageView (CornerRadius)
  20. /**
  21. * @brief init the Rounding UIImageView, no off-screen-rendered
  22. */
  23. - (instancetype)initWithRoundingRectImageView {
  24. self = [super init];
  25. if (self) {
  26. [self zy_cornerRadiusRoundingRect];
  27. }
  28. return self;
  29. }
  30. /**
  31. * @brief init the UIImageView with cornerRadius, no off-screen-rendered
  32. */
  33. - (instancetype)initWithCornerRadiusAdvance:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType {
  34. self = [super init];
  35. if (self) {
  36. [self zy_cornerRadiusAdvance:cornerRadius rectCornerType:rectCornerType];
  37. }
  38. return self;
  39. }
  40. /**
  41. * @brief attach border for UIImageView with width & color
  42. */
  43. - (void)zy_attachBorderWidth:(CGFloat)width color:(UIColor *)color {
  44. self.zyBorderWidth = width;
  45. self.zyBorderColor = color;
  46. }
  47. #pragma mark - Kernel
  48. /**
  49. * @brief clip the cornerRadius with image, UIImageView must be setFrame before, no off-screen-rendered
  50. */
  51. - (void)zy_cornerRadiusWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType {
  52. CGSize size = self.bounds.size;
  53. CGFloat scale = [UIScreen mainScreen].scale;
  54. CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);
  55. UIGraphicsBeginImageContextWithOptions(size, NO, scale);
  56. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  57. if (nil == currentContext) {
  58. return;
  59. }
  60. UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCornerType cornerRadii:cornerRadii];
  61. [cornerPath addClip];
  62. [self.layer renderInContext:currentContext];
  63. [self drawBorder:cornerPath];
  64. UIImage *processedImage = UIGraphicsGetImageFromCurrentImageContext();
  65. UIGraphicsEndImageContext();
  66. if (processedImage) {
  67. objc_setAssociatedObject(processedImage, &kProcessedImage, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  68. }
  69. self.image = processedImage;
  70. }
  71. /**
  72. * @brief clip the cornerRadius with image, draw the backgroundColor you want, UIImageView must be setFrame before, no off-screen-rendered, no Color Blended layers
  73. */
  74. - (void)zy_cornerRadiusWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType backgroundColor:(UIColor *)backgroundColor {
  75. CGSize size = self.bounds.size;
  76. CGFloat scale = [UIScreen mainScreen].scale;
  77. CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);
  78. UIGraphicsBeginImageContextWithOptions(size, YES, scale);
  79. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  80. if (nil == currentContext) {
  81. return;
  82. }
  83. UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCornerType cornerRadii:cornerRadii];
  84. UIBezierPath *backgroundRect = [UIBezierPath bezierPathWithRect:self.bounds];
  85. [backgroundColor setFill];
  86. [backgroundRect fill];
  87. [cornerPath addClip];
  88. [self.layer renderInContext:currentContext];
  89. [self drawBorder:cornerPath];
  90. UIImage *processedImage = UIGraphicsGetImageFromCurrentImageContext();
  91. UIGraphicsEndImageContext();
  92. if (processedImage) {
  93. objc_setAssociatedObject(processedImage, &kProcessedImage, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  94. }
  95. self.image = processedImage;
  96. }
  97. /**
  98. * @brief set cornerRadius for UIImageView, no off-screen-rendered
  99. */
  100. - (void)zy_cornerRadiusAdvance:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType {
  101. self.zyRadius = cornerRadius;
  102. self.roundingCorners = rectCornerType;
  103. self.zyIsRounding = NO;
  104. if (!self.zyHadAddObserver) {
  105. [[self class] swizzleDealloc];
  106. [self addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
  107. self.zyHadAddObserver = YES;
  108. }
  109. //Xcode 8 xib 删除了控件的Frame信息,需要主动创造
  110. [self layoutIfNeeded];
  111. }
  112. /**
  113. * @brief become Rounding UIImageView, no off-screen-rendered
  114. */
  115. - (void)zy_cornerRadiusRoundingRect {
  116. self.zyIsRounding = YES;
  117. if (!self.zyHadAddObserver) {
  118. [[self class] swizzleDealloc];
  119. [self addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
  120. self.zyHadAddObserver = YES;
  121. }
  122. //Xcode 8 xib 删除了控件的Frame信息,需要主动创造
  123. [self layoutIfNeeded];
  124. }
  125. #pragma mark - Private
  126. - (void)drawBorder:(UIBezierPath *)path {
  127. if (0 != self.zyBorderWidth && nil != self.zyBorderColor) {
  128. [path setLineWidth:2 * self.zyBorderWidth];
  129. [self.zyBorderColor setStroke];
  130. [path stroke];
  131. }
  132. }
  133. - (void)zy_dealloc {
  134. if (self.zyHadAddObserver) {
  135. [self removeObserver:self forKeyPath:@"image"];
  136. }
  137. [self zy_dealloc];
  138. }
  139. - (void)validateFrame {
  140. if (self.frame.size.width == 0) {
  141. [self.class swizzleLayoutSubviews];
  142. }
  143. }
  144. + (void)swizzleMethod:(SEL)oneSel anotherMethod:(SEL)anotherSel {
  145. Method oneMethod = class_getInstanceMethod(self, oneSel);
  146. Method anotherMethod = class_getInstanceMethod(self, anotherSel);
  147. method_exchangeImplementations(oneMethod, anotherMethod);
  148. }
  149. + (void)swizzleDealloc {
  150. static dispatch_once_t onceToken;
  151. dispatch_once(&onceToken, ^{
  152. [self swizzleMethod:NSSelectorFromString(@"dealloc") anotherMethod:@selector(zy_dealloc)];
  153. });
  154. }
  155. + (void)swizzleLayoutSubviews {
  156. static dispatch_once_t onceToken;
  157. dispatch_once(&onceToken, ^{
  158. [self swizzleMethod:@selector(layoutSubviews) anotherMethod:@selector(zy_LayoutSubviews)];
  159. });
  160. }
  161. - (void)zy_LayoutSubviews {
  162. [self zy_LayoutSubviews];
  163. if (self.zyIsRounding) {
  164. [self zy_cornerRadiusWithImage:self.image cornerRadius:self.frame.size.width/2 rectCornerType:UIRectCornerAllCorners];
  165. } else if (0 != self.zyRadius && 0 != self.roundingCorners && nil != self.image) {
  166. [self zy_cornerRadiusWithImage:self.image cornerRadius:self.zyRadius rectCornerType:self.roundingCorners];
  167. }
  168. }
  169. #pragma mark - KVO for .image
  170. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  171. if ([keyPath isEqualToString:@"image"]) {
  172. UIImage *newImage = change[NSKeyValueChangeNewKey];
  173. if ([newImage isMemberOfClass:[NSNull class]]) {
  174. return;
  175. } else if ([objc_getAssociatedObject(newImage, &kProcessedImage) intValue] == 1) {
  176. return;
  177. }
  178. [self validateFrame];
  179. if (self.zyIsRounding) {
  180. [self zy_cornerRadiusWithImage:newImage cornerRadius:self.frame.size.width/2 rectCornerType:UIRectCornerAllCorners];
  181. } else if (0 != self.zyRadius && 0 != self.roundingCorners && nil != self.image) {
  182. [self zy_cornerRadiusWithImage:newImage cornerRadius:self.zyRadius rectCornerType:self.roundingCorners];
  183. }
  184. }
  185. }
  186. #pragma mark property
  187. - (CGFloat)zyBorderWidth {
  188. return [objc_getAssociatedObject(self, _cmd) floatValue];
  189. }
  190. - (void)setZyBorderWidth:(CGFloat)zyBorderWidth {
  191. objc_setAssociatedObject(self, @selector(zyBorderWidth), @(zyBorderWidth), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  192. }
  193. - (UIColor *)zyBorderColor {
  194. return objc_getAssociatedObject(self, _cmd);
  195. }
  196. - (void)setZyBorderColor:(UIColor *)zyBorderColor {
  197. objc_setAssociatedObject(self, @selector(zyBorderColor), zyBorderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  198. }
  199. - (BOOL)zyHadAddObserver {
  200. return [objc_getAssociatedObject(self, _cmd) boolValue];
  201. }
  202. - (void)setZyHadAddObserver:(BOOL)zyHadAddObserver {
  203. objc_setAssociatedObject(self, @selector(zyHadAddObserver), @(zyHadAddObserver), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  204. }
  205. - (BOOL)zyIsRounding {
  206. return [objc_getAssociatedObject(self, _cmd) boolValue];
  207. }
  208. - (void)setZyIsRounding:(BOOL)zyIsRounding {
  209. objc_setAssociatedObject(self, @selector(zyIsRounding), @(zyIsRounding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  210. }
  211. - (UIRectCorner)roundingCorners {
  212. return [objc_getAssociatedObject(self, _cmd) unsignedLongValue];
  213. }
  214. - (void)setRoundingCorners:(UIRectCorner)roundingCorners {
  215. objc_setAssociatedObject(self, @selector(roundingCorners), @(roundingCorners), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  216. }
  217. - (CGFloat)zyRadius {
  218. return [objc_getAssociatedObject(self, _cmd) floatValue];
  219. }
  220. - (void)setZyRadius:(CGFloat)zyRadius {
  221. objc_setAssociatedObject(self, @selector(zyRadius), @(zyRadius), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  222. }
  223. @end
  224. //ZYCornerRadius is available under the MIT license.
  225. //Please visit https://github.com/liuzhiyi1992/ZYCornerRadius for details.