The most sexy way to use images in Swift.
- Create images with method chaining
- Create and manipulate images with CGContext
- Combine images with
+
operator - iOS support
- macOS support
UIImage.size(width: 100, height: 100)
.color(UIColor.whiteColor())
.border(color: UIColor.redColor())
.border(width: 10)
.corner(radius: 20)
.image
UIImage.resizable()
.color(UIColor.whiteColor())
.border(color: UIColor.blueColor())
.border(width: 5)
.corner(radius: 10)
.image
let image = UIImage(named: "myArrow").with(color: UIColor.blueColor())
SwiftyImage provides a simple way to create images with method chaining.
Method chaining starts from UIImage.size()
or UIImage.resizable()
.
UIImage.size(width: CGFloat, height: CGFloat) // ...
UIImage.size(size: CGSize) // ...
UIImage.resizable() // ...
You can set fill color, border attributes, corner radius, etc.
UIImage.size(width: 100, height: 100) // fixed size
.color(UIColor.whiteColor()) // fill color
.border(color: UIColor.redColor()) // border color
.border(width: 10) // border width
.corner(radius: 20) // corner radius
UIImage.resizable() // resizable image
.color(UIColor.whiteColor())
.border(color: UIColor.lightGrayColor())
.border(width: 1)
.corner(radius: 5)
Use .image
at the end of method chaining to generate image.
imageView.image = UIImage.size(width: 100, height: 100)
.color(UIColor.whiteColor())
.border(color: UIColor.redColor())
.border(width: 10)
.corner(radius: 20)
.image // generate UIImage
Start Chaining Method | Description |
---|---|
.size(width: CGFloat, height: CGFloat) |
Start chaining for fixed size image |
.size(CGSize) |
Start chaining for fixed size image |
.resizable() |
Start chaining for resizable image |
Setting Property Method | Description |
---|---|
.color(UIColor) |
Set fill color |
.border(width: CGFloat) |
Set border width |
.border(color: UIColor) |
Set border color |
.border(alignment: BorderAlignment) |
Set border alignment. Same with Photoshop's..Inside , .Center , .Outside |
.corner(radius: CGFloat) |
Set all corners radius of image |
.corner(topLeft: CGFloat) |
Set top left corner radius of image |
.corner(topRight: CGFloat) |
Set top right corner radius of image |
.corner(bottomLeft: CGFloat) |
Set bottom left corner radius of image |
.corner(bottomRight: CGFloat) |
Set bottom right corner radius of image |
Generating Image Method | Description |
---|---|
.image |
Generate and return image |
SwiftyImage also provides a simple method to create or manipulate images with 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))
}
You can easily combine multiple images with +
operator.
let backgroundImage = ...
let iconImage = ...
let combinedImage = backgroundImage + iconImage
-
For iOS 8+ projects: Use CocoaPods with Podfile:
pod 'SwiftyImage', '~> 1.0'
-
For iOS 7 projects: I recommend you to try CocoaSeeds, which uses source code instead of dynamic frameworks. Sample Seedfile:
github 'devxoul/SwiftyImage', '1.0.0', :files => 'Sources/SwiftyImage.swift'
Use CocoaPods command $ pod try SwiftyImage
to try Playground!
SwiftyImage is under MIT license. See the LICENSE file for more info.