SwiftyImage
在 Swift 中使用图像的最性感方式。
功能
- 使用方法链创建图像
- 渐变填充和描边
- 使用 CGContext 创建和管理图像
- 使用
+运算符组合图像 - 支持 iOS
- 支持 macOS
概览
创建图像
UIImage.size(width: 100, height: 100)
.color(.white)
.border(color: .red)
.border(width: 10)
.corner(radius: 20)
.imageUIImage.resizable()
.color(.white)
.border(color: .blue)
.border(width: 5)
.corner(radius: 10)
.image创建带有颜色遮罩的图像
let image = UIImage(named: "myArrow").with(color: UIColor.blueColor())入门指南
SwiftyImage 通过方法链提供创建图像的简单方法。
第 1 步:开始链式调用
方法链是从 UIImage.size() 或 UIImage.resizable() 开始的。
UIImage.size(width: CGFloat, height: CGFloat) // ...
UIImage.size(size: CGSize) // ...
UIImage.resizable() // ...第 2 步:设置属性
您可以设置填充颜色、边框属性、圆角半径等。
UIImage.size(width: 100, height: 100) // fixed size
.color(.white) // fill color
.border(color: .red) // border color
.border(width: 10) // border width
.corner(radius: 20) // corner radiusUIImage.resizable() // resizable image
.color(.white)
.border(color: .lightGray)
.border(width: 1)
.corner(radius: 5)第 3 步:生成图像
在方法链的末尾使用 .image 生成图像。
imageView.image = UIImage.size(width: 100, height: 100)
.color(.white)
.border(color: .red)
.border(width: 10)
.corner(radius: 20)
.image // generate UIImage可用的方法
开始方法链式调用
-
.size(width: CGFloat, height: CGFloat)从固定大小图像开始链式调用
-
.size(CGSize)从固定大小图像开始链式调用
-
.resizable()从可调整大小图像开始链式调用
设置属性
-
.color(UIColor)设置填充颜色
-
.color(gradient: [UIColor], locations: [CGFloat], from: CGPoint, to: CGPoint)设置渐变填充颜色
自版本 1.1.0 新增
-
.border(width: CGFloat)设置边框宽度
-
.border(color: UIColor)设置边框颜色
-
.border(gradient: [UIColor], locations: [CGFloat], from: CGPoint, to: CGPoint)设置渐变边框颜色
自版本 1.1.0 新增
-
.border(alignment: BorderAlignment)设置边框对齐。与 Photoshop 中的相同
可用值:
.inside,.center,.outside -
.corner(radius: CGFloat)设置图像的所有角落半径
-
.corner(topLeft: CGFloat)设置图像左上角的半径
-
.corner(topRight: CGFloat)设置图像右上角的半径
-
.corner(bottomLeft: CGFloat)设置图像左下角的半径
-
.corner(bottomRight: CGFloat)设置图像右下角的半径
生成图像
-
.image生成并返回图像
与 CGContext 交互
SwiftyImage 还提供了一个简单的方法来使用 CGContext 创建或编辑图像。
创建图像
let image = UIImage.with(size: CGSize(width: 100, height: 100)) { context in
UIColor.lightGrayColor().setFill()
CGContextFillEllipseInRect(context, CGRect(x: 0, y: 0, width: 100, height: 100))
}图像操作
let newImage = oldImage.with { context in
UIColor.lightGrayColor().setFill()
CGContextFillEllipseInRect(context, CGRect(x: 0, y: 0, width: 100, height: 100))
}图像操作符
您可以用+操作符轻松合并多个图像。
let backgroundImage = ...
let iconImage = ...
let combinedImage = backgroundImage + iconImage安装
pod 'SwiftyImage', '~> 1.1'沙盒
使用CocoaPods命令$ pod try SwiftyImage尝试沙盒!
许可
SwiftyImage遵循MIT许可协议。更多信息请参阅LICENSE文件。



