Skip to content

Commit c085e7a

Browse files
committed
新增 UIImage 添加圆角的 OC 写法
1 parent 436b9f7 commit c085e7a

5 files changed

Lines changed: 76 additions & 13 deletions

File tree

CornerRadius/CornerRadius.xcodeproj/project.pbxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
9B1F9F6D1C87E5440019AACD /* UIImage+ImageRoundedCorner.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F9F6C1C87E5440019AACD /* UIImage+ImageRoundedCorner.m */; };
1011
9B4915461C81D6080008161A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4915451C81D6080008161A /* AppDelegate.swift */; };
1112
9B4915481C81D6080008161A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4915471C81D6080008161A /* ViewController.swift */; };
1213
9B49154B1C81D6080008161A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B4915491C81D6080008161A /* Main.storyboard */; };
@@ -36,6 +37,9 @@
3637
/* End PBXContainerItemProxy section */
3738

3839
/* Begin PBXFileReference section */
40+
9B1F9F6A1C87E5430019AACD /* CornerRadius-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CornerRadius-Bridging-Header.h"; sourceTree = "<group>"; };
41+
9B1F9F6B1C87E5440019AACD /* UIImage+ImageRoundedCorner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+ImageRoundedCorner.h"; sourceTree = "<group>"; };
42+
9B1F9F6C1C87E5440019AACD /* UIImage+ImageRoundedCorner.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+ImageRoundedCorner.m"; sourceTree = "<group>"; };
3943
9B4915421C81D6080008161A /* CornerRadius.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CornerRadius.app; sourceTree = BUILT_PRODUCTS_DIR; };
4044
9B4915451C81D6080008161A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4145
9B4915471C81D6080008161A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -105,10 +109,13 @@
105109
9B4915471C81D6080008161A /* ViewController.swift */,
106110
9B4915731C81D77D0008161A /* CustomTableViewCell.swift */,
107111
9BADBA5D1C82B1B3000DB0E2 /* KtCorner.swift */,
112+
9B1F9F6B1C87E5440019AACD /* UIImage+ImageRoundedCorner.h */,
113+
9B1F9F6C1C87E5440019AACD /* UIImage+ImageRoundedCorner.m */,
108114
9B4915491C81D6080008161A /* Main.storyboard */,
109115
9B49154C1C81D6080008161A /* Assets.xcassets */,
110116
9B49154E1C81D6080008161A /* LaunchScreen.storyboard */,
111117
9B4915511C81D6080008161A /* Info.plist */,
118+
9B1F9F6A1C87E5430019AACD /* CornerRadius-Bridging-Header.h */,
112119
);
113120
path = CornerRadius;
114121
sourceTree = "<group>";
@@ -269,6 +276,7 @@
269276
9BADBA5E1C82B1B3000DB0E2 /* KtCorner.swift in Sources */,
270277
9B4915481C81D6080008161A /* ViewController.swift in Sources */,
271278
9B4915461C81D6080008161A /* AppDelegate.swift in Sources */,
279+
9B1F9F6D1C87E5440019AACD /* UIImage+ImageRoundedCorner.m in Sources */,
272280
);
273281
runOnlyForDeploymentPostprocessing = 0;
274282
};
@@ -408,21 +416,26 @@
408416
isa = XCBuildConfiguration;
409417
buildSettings = {
410418
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
419+
CLANG_ENABLE_MODULES = YES;
411420
INFOPLIST_FILE = CornerRadius/Info.plist;
412421
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
413422
PRODUCT_BUNDLE_IDENTIFIER = NEU.CornerRadius;
414423
PRODUCT_NAME = "$(TARGET_NAME)";
424+
SWIFT_OBJC_BRIDGING_HEADER = "CornerRadius/CornerRadius-Bridging-Header.h";
425+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
415426
};
416427
name = Debug;
417428
};
418429
9B49156C1C81D6080008161A /* Release */ = {
419430
isa = XCBuildConfiguration;
420431
buildSettings = {
421432
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
433+
CLANG_ENABLE_MODULES = YES;
422434
INFOPLIST_FILE = CornerRadius/Info.plist;
423435
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
424436
PRODUCT_BUNDLE_IDENTIFIER = NEU.CornerRadius;
425437
PRODUCT_NAME = "$(TARGET_NAME)";
438+
SWIFT_OBJC_BRIDGING_HEADER = "CornerRadius/CornerRadius-Bridging-Header.h";
426439
};
427440
name = Release;
428441
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+
5+
#import "UIImage+ImageRoundedCorner.h"

CornerRadius/CornerRadius/KtCorner.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,25 @@ extension UIView {
5757
backgroundColor: UIColor,
5858
borderColor: UIColor) -> UIImage {
5959
let sizeToFit = CGSize(width: pixel(Double(self.bounds.size.width)), height: Double(self.bounds.size.height))
60-
let halfBorderWidth = CGFloat(borderWidth / 2.0);
60+
let halfBorderWidth = CGFloat(borderWidth / 2.0)
6161

6262
UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.mainScreen().scale)
6363
let context = UIGraphicsGetCurrentContext()
6464

65-
CGContextSetLineWidth(context, borderWidth);
66-
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
67-
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
65+
CGContextSetLineWidth(context, borderWidth)
66+
CGContextSetStrokeColorWithColor(context, borderColor.CGColor)
67+
CGContextSetFillColorWithColor(context, backgroundColor.CGColor)
6868

6969
let width = sizeToFit.width, height = sizeToFit.height
70-
CGContextMoveToPoint(context, width - halfBorderWidth, radius + halfBorderWidth); // 开始坐标右边开始
71-
CGContextAddArcToPoint(context, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius); // 右下角角度
72-
CGContextAddArcToPoint(context, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius); // 左下角角度
73-
CGContextAddArcToPoint(context, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius); // 左上角
74-
CGContextAddArcToPoint(context, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius); // 右上角
70+
CGContextMoveToPoint(context, width - halfBorderWidth, radius + halfBorderWidth) // 开始坐标右边开始
71+
CGContextAddArcToPoint(context, width - halfBorderWidth, height - halfBorderWidth, width - radius - halfBorderWidth, height - halfBorderWidth, radius) // 右下角角度
72+
CGContextAddArcToPoint(context, halfBorderWidth, height - halfBorderWidth, halfBorderWidth, height - radius - halfBorderWidth, radius) // 左下角角度
73+
CGContextAddArcToPoint(context, halfBorderWidth, halfBorderWidth, width - halfBorderWidth, halfBorderWidth, radius) // 左上角
74+
CGContextAddArcToPoint(context, width - halfBorderWidth, halfBorderWidth, width - halfBorderWidth, radius + halfBorderWidth, radius) // 右上角
7575

7676
CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke)
77-
let output = UIGraphicsGetImageFromCurrentImageContext();
78-
UIGraphicsEndImageContext();
77+
let output = UIGraphicsGetImageFromCurrentImageContext()
78+
UIGraphicsEndImageContext()
7979
return output
8080
}
8181
}
@@ -87,6 +87,8 @@ extension UIImageView {
8787
:param: radius 圆角半径
8888
*/
8989
override func kt_addCorner(radius radius: CGFloat) {
90+
// 被注释的是图片添加圆角的 OC 写法
91+
// self.image = self.image?.imageAddCornerWithRadius(radius, andSize: self.bounds.size)
9092
self.image = self.image?.kt_drawRectWithRoundedCorner(radius: radius, self.bounds.size)
9193
}
9294
}
@@ -103,8 +105,8 @@ extension UIImage {
103105

104106
self.drawInRect(rect)
105107
CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke)
106-
let output = UIGraphicsGetImageFromCurrentImageContext();
107-
UIGraphicsEndImageContext();
108+
let output = UIGraphicsGetImageFromCurrentImageContext()
109+
UIGraphicsEndImageContext()
108110

109111
return output
110112
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// UIImage+ImageRoundedCorner.h
3+
// CornerRadius
4+
//
5+
// Created by 张星宇 on 16/3/3.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIImage (ImageRoundedCorner)
12+
13+
- (UIImage*)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size;
14+
15+
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// UIImage+ImageRoundedCorner.m
3+
// CornerRadius
4+
//
5+
// Created by 张星宇 on 16/3/3.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "UIImage+ImageRoundedCorner.h"
10+
11+
@implementation UIImage (ImageRoundedCorner)
12+
13+
- (UIImage*)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size{
14+
CGRect rect = CGRectMake(0, 0, size.width, size.height);
15+
16+
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
17+
CGContextRef ctx = UIGraphicsGetCurrentContext();
18+
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
19+
CGContextAddPath(ctx,path.CGPath);
20+
CGContextClip(ctx);
21+
[self drawInRect:rect];
22+
CGContextDrawPath(ctx, kCGPathFillStroke);
23+
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
24+
UIGraphicsEndImageContext();
25+
return newImage;
26+
}
27+
28+
@end

0 commit comments

Comments
 (0)