Skip to content

Commit

Permalink
added another path-based masking method
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Kirchner committed Jul 20, 2015
1 parent 7e3146a commit c773a68
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Toucan.xcworkspace/xcuserdata/bunneyapps.xcuserdatad
build
.idea
docs

Toucan.xcworkspace/xcuserdata/bkirchner.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
30 changes: 29 additions & 1 deletion Source/Toucan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ public class Toucan : NSObject {
Mask the contained image with a path (UIBezierPath) that will be scaled to fit the image.

- parameter path: UIBezierPath to mask the image
- parameter borderColor: Optional color of the border - default White

- returns: Self, allowing method chaining
*/
Expand All @@ -228,6 +227,18 @@ public class Toucan : NSObject {
return self
}

/**
Mask the contained image with a path (UIBezierPath) which is provided via a closure.

- parameter path: closure that returns a UIBezierPath. Using a closure allows the user to provide the path after knowing the size of the image

- returns: Self, allowing method chaining
*/
public func maskWithPathClosure(#path: (rect: CGRect) -> (UIBezierPath)) -> Toucan {
self.image = Toucan.Mask.maskImageWithPathClosure(self.image, pathInRect: path)
return self
}

/**
Mask the contained image with a rounded rectangle border.
Allows specifying an additional border to draw on the clipped image.
Expand Down Expand Up @@ -365,6 +376,23 @@ public class Toucan : NSObject {
}
}

/**
Mask the given image with a path(UIBezierPath) provided via a closure. This allows the user to get the size of the image before computing their path variable.

- parameter image: Image to apply the mask to
- parameter path: UIBezierPath to make as the mask

- returns: Masked image
*/
public static func maskImageWithPathClosure(image: UIImage,
pathInRect:(rect: CGRect) -> (UIBezierPath)) -> UIImage {

let imgRef = Util.CGImageWithCorrectOrientation(image)
let size = CGSize(width: CGFloat(CGImageGetWidth(imgRef)) / image.scale, height: CGFloat(CGImageGetHeight(imgRef)) / image.scale)

return maskImageWithPath(image, path: pathInRect(rect: CGRectMake(0, 0, size.width, size.height)))
}

/**
Mask the given image with a rounded rectangle border.
Allows specifying an additional border to draw on the clipped image.
Expand Down
10 changes: 9 additions & 1 deletion Tests/MaskingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class MaskingTests : ToucanTestCase {
path.addLineToPoint(CGPointMake(100, 50))
path.addLineToPoint(CGPointMake(50, 100))
path.closePath()
let masked = Toucan(image: landscapeImage).maskWithPath(path: path).image
let masked2 = Toucan(image: landscapeImage).resize(CGSizeMake(300, 250), fitMode: Toucan.Resize.FitMode.Scale).maskWithPath(path: path).image

let cornerRGBA = getPixelRGBA(masked2, point: CGPoint(x: 0, y: 0))
XCTAssertEqual(cornerRGBA.alpha, 0.0 as CGFloat, "Check corner is transparent")

// let centerRGBA = getPixelRGBA(masked2, point: CGPoint(x: masked2.size.width / 2, y: masked2.size.height / 2))

let centerRGBA = getPixelRGBA(masked2, point: CGPoint(x: 150, y: 125))
XCTAssertEqual(centerRGBA.alpha, 255.0 as CGFloat, "Check center is not transparent")
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions ToucanPlayground.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ path.addLineToPoint(CGPointMake(50, 100))
path.closePath()
Toucan(image: landscapeCropped).maskWithPath(path: path).image

Toucan(image: landscapeCropped).maskWithPathClosure(path: {(rect) -> (UIBezierPath) in
return UIBezierPath(roundedRect: rect, cornerRadius: 50.0)
}).image


// ------------------------------------------------------------
// Layers
Expand Down
7 changes: 6 additions & 1 deletion ToucanPlayground.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=62&amp;CharacterRangeLoc=3548&amp;EndingColumnNumber=63&amp;EndingLineNumber=80&amp;StartingColumnNumber=1&amp;StartingLineNumber=80&amp;Timestamp=459098076.528566"
documentLocation = "#CharacterRangeLen=62&amp;CharacterRangeLoc=3548&amp;EndingColumnNumber=63&amp;EndingLineNumber=80&amp;StartingColumnNumber=1&amp;StartingLineNumber=80&amp;Timestamp=459113115.371909"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=158&amp;CharacterRangeLoc=3612&amp;EndingColumnNumber=9&amp;EndingLineNumber=84&amp;StartingColumnNumber=1&amp;StartingLineNumber=82&amp;Timestamp=459113145.238215"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand Down

0 comments on commit c773a68

Please sign in to comment.