Skip to content

Commit

Permalink
Merge pull request #7 from devxoul/color-overlay
Browse files Browse the repository at this point in the history
Add support for creating color overlayed image.
  • Loading branch information
devxoul committed Jan 21, 2016
2 parents 8b6a349 + 77410ae commit 3af46d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
19 changes: 18 additions & 1 deletion SwiftyImage.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ UIImage.with(size: CGSize(width: 100, height: 100)) { context in
.image()
let clock = background + circle + center
return clock.with { context in
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineCap(context, .Round)

UIColor.blackColor().setStroke()
CGContextSetLineWidth(context, 2)
Expand Down Expand Up @@ -121,3 +121,20 @@ UIImage.with(size: CGSize(width: 100, height: 100)) { context in
.image()
return background + circle
})()


//: Color overlay
//: -------------

({ () -> UIImage in
let image = UIImage.size(width: 100, height: 100)
.color(UIColor.whiteColor())
.border(color: UIColor.blueColor())
.border(width: 10)
.border(alignment: .Outside)
.corner(topLeft: 20)
.corner(topRight: 15)
.corner(bottomRight: 50)
.image()
return image.with(color: .redColor())
})()
15 changes: 10 additions & 5 deletions SwiftyImage.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4241&amp;EndingColumnNumber=80&amp;EndingLineNumber=57&amp;StartingColumnNumber=1&amp;StartingLineNumber=57&amp;Timestamp=461496012.286888"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4766&amp;EndingColumnNumber=80&amp;EndingLineNumber=57&amp;StartingColumnNumber=1&amp;StartingLineNumber=57&amp;Timestamp=475059300.458425"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand Down Expand Up @@ -62,7 +62,7 @@
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=6&amp;CharacterRangeLoc=4233&amp;EndingColumnNumber=7&amp;EndingLineNumber=143&amp;StartingColumnNumber=1&amp;StartingLineNumber=143&amp;Timestamp=461496012.288939"
documentLocation = "#CharacterRangeLen=6&amp;CharacterRangeLoc=4758&amp;EndingColumnNumber=7&amp;EndingLineNumber=160&amp;StartingColumnNumber=1&amp;StartingLineNumber=160&amp;Timestamp=475059300.459732"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand All @@ -72,12 +72,12 @@
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=1077&amp;CharacterRangeLoc=2149&amp;EndingColumnNumber=10&amp;EndingLineNumber=79&amp;StartingColumnNumber=1&amp;StartingLineNumber=79&amp;Timestamp=461496012.289503"
documentLocation = "#CharacterRangeLen=1068&amp;CharacterRangeLoc=2149&amp;EndingColumnNumber=10&amp;EndingLineNumber=79&amp;StartingColumnNumber=1&amp;StartingLineNumber=79&amp;Timestamp=475059194.251142"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=1663&amp;CharacterRangeLoc=1563&amp;EndingColumnNumber=5&amp;EndingLineNumber=68&amp;StartingColumnNumber=1&amp;StartingLineNumber=67&amp;Timestamp=461496012.289786"
documentLocation = "#CharacterRangeLen=1654&amp;CharacterRangeLoc=1563&amp;EndingColumnNumber=5&amp;EndingLineNumber=68&amp;StartingColumnNumber=1&amp;StartingLineNumber=67&amp;Timestamp=475059194.251331"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand All @@ -97,7 +97,7 @@
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=443&amp;CharacterRangeLoc=3268&amp;EndingColumnNumber=5&amp;EndingLineNumber=122&amp;StartingColumnNumber=1&amp;StartingLineNumber=112&amp;Timestamp=461496012.290896"
documentLocation = "#CharacterRangeLen=443&amp;CharacterRangeLoc=3259&amp;EndingColumnNumber=5&amp;EndingLineNumber=122&amp;StartingColumnNumber=1&amp;StartingLineNumber=112&amp;Timestamp=475059194.252074"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand All @@ -107,5 +107,10 @@
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=494&amp;CharacterRangeLoc=3742&amp;EndingColumnNumber=5&amp;EndingLineNumber=137&amp;StartingColumnNumber=1&amp;StartingLineNumber=137&amp;Timestamp=475059300.461463"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
19 changes: 19 additions & 0 deletions SwiftyImage/SwiftyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,22 @@ public class ImageDrawer {
return image
}
}


// MARK: - Color overlay

public extension UIImage {

public func with(color color: UIColor) -> UIImage {
return UIImage.with(size: self.size) { context in
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1, -1)
CGContextSetBlendMode(context, .Normal)
let rect = CGRect(origin: .zero, size: self.size)
CGContextClipToMask(context, rect, self.CGImage)
color.setFill()
CGContextFillRect(context, rect)
}
}

}

0 comments on commit 3af46d8

Please sign in to comment.