This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Added ability to lock the height/width ration when cropping #9
Open
perfaram
wants to merge
4
commits into
justwudi:master
Choose a base branch
from
perfaram:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7b9c83
Added ability to lock the height/width ration when cropping
perfaram b47ab27
Now possible to request only the selected image and rectangles, inste…
perfaram 6af1d6d
To Swift 3.0
perfaram 4804f20
Dismissing the view controller failed on iPads
perfaram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
private var anchor: CGPoint! | ||
private var startPoint: CGPoint! | ||
private var resizeMultiplyer = WDResizableViewBorderMultiplyer() | ||
private var lockAspectRatio: Bool = false | ||
|
||
override var frame: CGRect { | ||
get { | ||
|
@@ -52,9 +53,9 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
} | ||
} | ||
|
||
init(frame: CGRect, initialContentSize: CGSize) { | ||
init(frame: CGRect, initialContentSize: CGSize, lockAspectRatio locked: Bool = false) { | ||
super.init(frame: frame) | ||
|
||
self.lockAspectRatio = locked | ||
self.initialContentSize = initialContentSize | ||
self.addContentViews() | ||
} | ||
|
@@ -114,7 +115,7 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
(width - initialContentSize.width) / 2 - kBorderCorrectionValue, | ||
(height - toolbarSize - initialContentSize.height) / 2 - kBorderCorrectionValue, | ||
initialContentSize.width + kBorderCorrectionValue * 2, | ||
initialContentSize.height + kBorderCorrectionValue * 2)) | ||
initialContentSize.height + kBorderCorrectionValue * 2), lockAspectRatio: self.lockAspectRatio) | ||
self.addSubview(cropBorderView) | ||
} | ||
|
||
|
@@ -145,23 +146,26 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
let bottomY = cropBorderView.bounds.size.height | ||
let middleY = topY + (bottomY - topY) / 2 | ||
|
||
// starting with the upper left corner and then following the rect clockwise | ||
let topLeft = CGPointMake(leftX, topY) | ||
let topCenter = CGPointMake(centerX, topY) | ||
let topRight = CGPointMake(rightX, topY) | ||
let middleRight = CGPointMake(rightX, middleY) | ||
let bottomRight = CGPointMake(rightX, bottomY) | ||
let bottomCenter = CGPointMake(centerX, bottomY) | ||
let bottomLeft = CGPointMake(leftX, bottomY) | ||
let middleLeft = CGPointMake(leftX, middleY) | ||
|
||
return [topLeft, topCenter, topRight, middleRight, bottomRight, bottomCenter, bottomLeft, | ||
middleLeft] | ||
var handleArray = [CGPoint]() | ||
handleArray.append(CGPointMake(leftX, topY)) //top left | ||
handleArray.append(CGPointMake(rightX, topY)) //top right | ||
handleArray.append(CGPointMake(rightX, bottomY)) //bottom right | ||
handleArray.append(CGPointMake(leftX, bottomY)) //bottom left | ||
|
||
if !lockAspectRatio { | ||
handleArray.append(CGPointMake(centerX, topY)) //top center | ||
handleArray.append(CGPointMake(rightX, middleY)) //middle right | ||
handleArray.append(CGPointMake(centerX, bottomY)) //bottom center | ||
handleArray.append(CGPointMake(leftX, middleY)) //middle left | ||
} | ||
|
||
return handleArray | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When aspect ratio is locked, show only the handles located at the angles. |
||
} | ||
|
||
private func resizeWithTouchPoint(point: CGPoint) { | ||
// This is the place where all the magic happends | ||
// prevent goint offscreen... | ||
|
||
let border = kBorderCorrectionValue * 2 | ||
var pointX = point.x < border ? border : point.x | ||
var pointY = point.y < border ? border : point.y | ||
|
@@ -170,11 +174,19 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
pointY = pointY > self.superview!.bounds.size.height - border ? | ||
self.superview!.bounds.size.height - border : pointY | ||
|
||
let heightChange = (pointY - startPoint.y) * resizeMultiplyer.heightMultiplyer | ||
let widthChange = (startPoint.x - pointX) * resizeMultiplyer.widthMultiplyer | ||
var heightChange = (pointY - startPoint.y) * resizeMultiplyer.heightMultiplyer | ||
var widthChange = (startPoint.x - pointX) * resizeMultiplyer.widthMultiplyer | ||
|
||
if lockAspectRatio { | ||
let averageChange = (heightChange + widthChange) / 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ensures that the handle currently used for resizing doesn't get too far from the user's touch point. |
||
heightChange = averageChange | ||
widthChange = averageChange | ||
} | ||
|
||
let xChange = -1 * widthChange * resizeMultiplyer.xMultiplyer | ||
let yChange = -1 * heightChange * resizeMultiplyer.yMultiplyer | ||
|
||
|
||
var newFrame = CGRectMake( | ||
cropBorderView.frame.origin.x + xChange, | ||
cropBorderView.frame.origin.y + yChange, | ||
|
@@ -192,31 +204,62 @@ internal class WDResizableCropOverlayView: WDImageCropOverlayView { | |
if newFrame.size.width < 64 { | ||
newFrame.size.width = cropBorderView.frame.size.width | ||
newFrame.origin.x = cropBorderView.frame.origin.x | ||
if lockAspectRatio { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Equilibrates changes between width & height to keep ratio (just like all other changes made in this function). |
||
newFrame.size.height = cropBorderView.frame.size.height | ||
newFrame.origin.y = cropBorderView.frame.origin.y | ||
} | ||
} | ||
|
||
if newFrame.size.height < 64 { | ||
newFrame.size.height = cropBorderView.frame.size.height | ||
newFrame.origin.y = cropBorderView.frame.origin.y | ||
if lockAspectRatio { | ||
newFrame.size.width = cropBorderView.frame.size.width | ||
newFrame.origin.x = cropBorderView.frame.origin.x | ||
} | ||
} | ||
|
||
if newFrame.origin.x < 0 { | ||
newFrame.size.width = cropBorderView.frame.size.width + | ||
(cropBorderView.frame.origin.x - self.superview!.bounds.origin.x) | ||
newFrame.origin.x = 0 | ||
if lockAspectRatio { | ||
newFrame.size.height = cropBorderView.frame.size.height + | ||
(cropBorderView.frame.origin.y - self.superview!.bounds.origin.y) | ||
newFrame.origin.y = 0 | ||
} | ||
} | ||
|
||
if newFrame.origin.y < 0 { | ||
newFrame.size.height = cropBorderView.frame.size.height + | ||
(cropBorderView.frame.origin.y - self.superview!.bounds.origin.y) | ||
newFrame.origin.y = 0 | ||
if lockAspectRatio { | ||
newFrame.size.width = cropBorderView.frame.size.width + | ||
(cropBorderView.frame.origin.x - self.superview!.bounds.origin.x) | ||
newFrame.origin.x = 0 | ||
} | ||
} | ||
|
||
if newFrame.size.width + newFrame.origin.x > self.frame.size.width { | ||
newFrame.size.width = self.frame.size.width - cropBorderView.frame.origin.x | ||
if lockAspectRatio { | ||
newFrame.size.width = cropBorderView.frame.size.width | ||
newFrame.size.height = cropBorderView.frame.size.height | ||
} | ||
else { | ||
newFrame.size.width = self.frame.size.width - cropBorderView.frame.origin.x | ||
} | ||
} | ||
|
||
if newFrame.size.height + newFrame.origin.y > self.frame.size.height - toolbarSize { | ||
newFrame.size.height = self.frame.size.height - | ||
cropBorderView.frame.origin.y - toolbarSize | ||
if lockAspectRatio { | ||
newFrame.size.width = cropBorderView.frame.size.width | ||
newFrame.size.height = cropBorderView.frame.size.height | ||
} | ||
else { | ||
newFrame.size.height = self.frame.size.height - | ||
cropBorderView.frame.origin.y - toolbarSize | ||
} | ||
} | ||
|
||
return newFrame | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When aspect ratio is locked, show only the handles located at the angles.