Skip to content

Commit fd86a92

Browse files
committed
Added lockSquare feature for iOS
1 parent 0da64f9 commit fd86a92

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Option | Type | Description
9696
------ | ------ | ------------------------------------------------
9797
width | number | The width of the image you would like returned.
9898
height | number | The height of the image you would like returned.
99+
lockSquare | boolean | Pass this as true, to lock square aspect ratio on iOS, on android, this option doesn't make any difference.
99100

100101
### Additional notes for Android
101102
You can override library colors just specifying colors with the same names in your colors.xml file.

src/imagecropper.ios.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ declare class TOCropViewController extends UIViewController {
2020
delegate: any;
2121
cropView: TOCropView;
2222
toolbar: TOCropToolbar;
23+
defaultAspectRatio: any;
24+
aspectRatioLocked: boolean;
2325
public setAspectRatioLocked(aspectRatioLocked: boolean): void;
2426
public setRotateButtonHidden(rotateButtonHidden: boolean): void;
2527
public setRotateClockwiseButtonHidden(rotateClockwiseButtonHidden: boolean): void;
@@ -115,21 +117,22 @@ export class ImageCropper {
115117
if (image.ios) {
116118
var viewController = TOCropViewController.alloc().initWithImage(image.ios);
117119
var delegate = TOCropViewControllerDelegateImpl.initWithOwner(new WeakRef(viewController));
118-
119120
delegate.initResolveReject(resolve, reject);
120-
121121
CFRetain(delegate);
122122
viewController.delegate = delegate;
123-
124123
var page = frame.topmost().ios.controller;
124+
if (_options.lockSquare) {
125+
viewController.defaultAspectRatio = 1;
126+
viewController.aspectRatioLocked = true; // The crop box is locked to the aspect ratio and can't be resized away from it
127+
}
125128
page.presentViewControllerAnimatedCompletion(viewController, true, function () {
126-
//Set Fixed Crop Size
127-
if (_options && _options.width && _options.height) {
128-
var gcd = _that._gcd(_options.width, _options.height);
129+
if (_options) {
130+
if (_options.width && _options.height) {
131+
var gcd = _that._gcd(_options.width, _options.height);
132+
viewController.toolbar.clampButtonHidden = true;
133+
viewController.cropView.setAspectLockEnabledWithAspectRatioAnimated(CGSizeMake(_options.width / gcd, _options.height / gcd), false);
134+
}
129135

130-
viewController.toolbar.clampButtonHidden = true;
131-
// viewController.toolbar.setNeedsLayout();
132-
viewController.cropView.setAspectLockEnabledWithAspectRatioAnimated(CGSizeMake(_options.width / gcd, _options.height / gcd), false);
133136
}
134137
});
135138
}

src/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as imageSource from 'tns-core-modules/image-source';
22

33
export interface OptionsCommon{
44
width?: number,
5-
height?: number
5+
height?: number,
6+
lockSquare?: number
67
}
78

89
export interface Result{

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-imagecropper",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "NativeScript Image Cropper Plugin",
55
"main": "imagecropper",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)