浏览代码

add image border color process method

ibireme 9 年之前
父节点
当前提交
e8009ae33b
共有 3 个文件被更改,包括 76 次插入23 次删除
  1. 16 16
      README.md
  2. 27 4
      YYWebImage/Categories/UIImage+YYWebImage.h
  3. 33 3
      YYWebImage/Categories/UIImage+YYWebImage.m

+ 16 - 16
README.md

@@ -103,14 +103,14 @@ Installation
 1. Download all the files in the YYWebImage subdirectory.
 1. Download all the files in the YYWebImage subdirectory.
 2. Add the source files to your Xcode project.
 2. Add the source files to your Xcode project.
 3. Link with required frameworks:
 3. Link with required frameworks:
-	* UIKit.framework
-	* CoreFoundation.framework
-	* QuartzCore.framework
-	* AssetsLibrary.framework
-	* ImageIO.framework
-	* Accelerate.framework
-	* MobileCoreServices.framework
-	* libsqlite3
+	* UIKit
+	* CoreFoundation
+	* QuartzCore
+	* AssetsLibrary
+	* ImageIO
+	* Accelerate
+	* MobileCoreServices
+	* sqlite3
 	* libz
 	* libz
 4. Add `Vendor/WebP.framework`(static library) to your Xcode project if you want to support webp.
 4. Add `Vendor/WebP.framework`(static library) to your Xcode project if you want to support webp.
 5. Import `YYWebImage.h`.
 5. Import `YYWebImage.h`.
@@ -232,14 +232,14 @@ YYWebImage 是一个异步图片加载框架 ([YYKit](https://github.com/ibireme
 1. 下载 YYWebImage 文件夹内的所有内容。
 1. 下载 YYWebImage 文件夹内的所有内容。
 2. 将 YYWebModel 内的源文件添加(拖放)到你的工程。
 2. 将 YYWebModel 内的源文件添加(拖放)到你的工程。
 3. 链接以下 frameworks:
 3. 链接以下 frameworks:
-	* UIKit.framework
-	* CoreFoundation.framework
-	* QuartzCore.framework
-	* AssetsLibrary.framework
-	* ImageIO.framework
-	* Accelerate.framework
-	* MobileCoreServices.framework
-	* libsqlite3
+	* UIKit
+	* CoreFoundation
+	* QuartzCore
+	* AssetsLibrary
+	* ImageIO
+	* Accelerate
+	* MobileCoreServices
+	* sqlite3
 	* libz
 	* libz
 4. 如果你需要支持 webp,可以将 `Vendor/WebP.framework`(静态库) 加入你的工程。
 4. 如果你需要支持 webp,可以将 `Vendor/WebP.framework`(静态库) 加入你的工程。
 5. 导入 `YYWebImage.h`。
 5. 导入 `YYWebImage.h`。

+ 27 - 4
YYWebImage/Categories/UIImage+YYWebImage.h

@@ -150,6 +150,23 @@
  */
  */
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius;
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius;
 
 
+/**
+ Rounds a new image with a given corner size.
+ 
+ @param radius       The radius of each corner oval. Values larger than half the
+                     rectangle's width or height are clamped appropriately to
+                     half the width or height.
+
+ @param borderWidth  The inset border line width. Values larger than half the rectangle's
+                     width or height are clamped appropriately to half the width 
+                     or height.
+ 
+ @param borderColor  The border stroke color. nil means clear color.
+ */
+- (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
+                             borderWidth:(CGFloat)borderWidth
+                             borderColor:(UIColor *)borderColor;
+
 /**
 /**
  Rounds a new image with a given corner size.
  Rounds a new image with a given corner size.
  
  
@@ -161,13 +178,19 @@
                      rounded. You can use this parameter to round only a subset
                      rounded. You can use this parameter to round only a subset
                      of the corners of the rectangle.
                      of the corners of the rectangle.
  
  
- @param borderWidth  The inset border with clear color. Values larger than half
-                     the rectangle's width or height are clamped appropriately
-                     to half the width or height.
+ @param borderWidth  The inset border line width. Values larger than half the rectangle's
+                     width or height are clamped appropriately to half the width 
+                     or height.
+ 
+ @param borderColor  The border stroke color. nil means clear color.
+ 
+ @param borderLineJoin The border line join.
  */
  */
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
                                  corners:(UIRectCorner)corners
                                  corners:(UIRectCorner)corners
-                             borderWidth:(CGFloat)borderWidth;
+                             borderWidth:(CGFloat)borderWidth
+                             borderColor:(UIColor *)borderColor
+                          borderLineJoin:(CGLineJoin)borderLineJoin;
 
 
 /**
 /**
  Returns a new rotated image (relative to the center).
  Returns a new rotated image (relative to the center).

+ 33 - 3
YYWebImage/Categories/UIImage+YYWebImage.m

@@ -337,10 +337,21 @@ static NSTimeInterval _yy_CGImageSourceGetGIFFrameDelayAtIndex(CGImageSourceRef
 }
 }
 
 
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius {
 - (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius {
-    return [self yy_imageByRoundCornerRadius:radius corners:UIRectCornerAllCorners borderWidth:0];
+    return [self yy_imageByRoundCornerRadius:radius borderWidth:0 borderColor:nil];
 }
 }
 
 
-- (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius corners:(UIRectCorner)corners borderWidth:(CGFloat)borderWidth {
+- (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
+                             borderWidth:(CGFloat)borderWidth
+                             borderColor:(UIColor *)borderColor {
+    return [self yy_imageByRoundCornerRadius:radius corners:UIRectCornerAllCorners borderWidth:borderWidth borderColor:borderColor borderLineJoin:kCGLineJoinMiter];
+}
+
+- (UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
+                                 corners:(UIRectCorner)corners
+                             borderWidth:(CGFloat)borderWidth
+                             borderColor:(UIColor *)borderColor
+                          borderLineJoin:(CGLineJoin)borderLineJoin {
+    
     UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
     UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
     CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
@@ -349,9 +360,28 @@ static NSTimeInterval _yy_CGImageSourceGetGIFFrameDelayAtIndex(CGImageSourceRef
     
     
     CGFloat minSize = MIN(self.size.width, self.size.height);
     CGFloat minSize = MIN(self.size.width, self.size.height);
     if (borderWidth < minSize / 2) {
     if (borderWidth < minSize / 2) {
-        [[UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, borderWidth)] addClip];
+        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, borderWidth)];
+        [path closePath];
+        
+        CGContextSaveGState(context);
+        [path addClip];
         CGContextDrawImage(context, rect, self.CGImage);
         CGContextDrawImage(context, rect, self.CGImage);
+        CGContextRestoreGState(context);
     }
     }
+    
+    if (borderColor && borderWidth < minSize / 2 && borderWidth > 0) {
+        CGFloat strokeInset = (floor(borderWidth * self.scale) + 0.5) / self.scale;
+        CGRect strokeRect = CGRectInset(rect, strokeInset, strokeInset);
+        CGFloat strokeRadius = radius > self.scale / 2 ? radius - self.scale / 2 : 0;
+        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, borderWidth)];
+        [path closePath];
+        
+        path.lineWidth = borderWidth;
+        path.lineJoinStyle = borderLineJoin;
+        [borderColor setStroke];
+        [path stroke];
+    }
+    
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     UIGraphicsEndImageContext();
     return image;
     return image;