Skip to content

Commit

Permalink
Added lockSquare feature for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
shiv19 committed Oct 20, 2017
1 parent 0da64f9 commit fd86a92
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Option | Type | Description
------ | ------ | ------------------------------------------------
width | number | The width of the image you would like returned.
height | number | The height of the image you would like returned.
lockSquare | boolean | Pass this as true, to lock square aspect ratio on iOS, on android, this option doesn't make any difference.

### Additional notes for Android
You can override library colors just specifying colors with the same names in your colors.xml file.
Expand Down
21 changes: 12 additions & 9 deletions src/imagecropper.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ declare class TOCropViewController extends UIViewController {
delegate: any;
cropView: TOCropView;
toolbar: TOCropToolbar;
defaultAspectRatio: any;
aspectRatioLocked: boolean;
public setAspectRatioLocked(aspectRatioLocked: boolean): void;
public setRotateButtonHidden(rotateButtonHidden: boolean): void;
public setRotateClockwiseButtonHidden(rotateClockwiseButtonHidden: boolean): void;
Expand Down Expand Up @@ -115,21 +117,22 @@ export class ImageCropper {
if (image.ios) {
var viewController = TOCropViewController.alloc().initWithImage(image.ios);
var delegate = TOCropViewControllerDelegateImpl.initWithOwner(new WeakRef(viewController));

delegate.initResolveReject(resolve, reject);

CFRetain(delegate);
viewController.delegate = delegate;

var page = frame.topmost().ios.controller;
if (_options.lockSquare) {
viewController.defaultAspectRatio = 1;
viewController.aspectRatioLocked = true; // The crop box is locked to the aspect ratio and can't be resized away from it
}
page.presentViewControllerAnimatedCompletion(viewController, true, function () {
//Set Fixed Crop Size
if (_options && _options.width && _options.height) {
var gcd = _that._gcd(_options.width, _options.height);
if (_options) {
if (_options.width && _options.height) {
var gcd = _that._gcd(_options.width, _options.height);
viewController.toolbar.clampButtonHidden = true;
viewController.cropView.setAspectLockEnabledWithAspectRatioAnimated(CGSizeMake(_options.width / gcd, _options.height / gcd), false);
}

viewController.toolbar.clampButtonHidden = true;
// viewController.toolbar.setNeedsLayout();
viewController.cropView.setAspectLockEnabledWithAspectRatioAnimated(CGSizeMake(_options.width / gcd, _options.height / gcd), false);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as imageSource from 'tns-core-modules/image-source';

export interface OptionsCommon{
width?: number,
height?: number
height?: number,
lockSquare?: number
}

export interface Result{
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-imagecropper",
"version": "0.1.1",
"version": "0.1.2",
"description": "NativeScript Image Cropper Plugin",
"main": "imagecropper",
"typings": "index.d.ts",
Expand Down

0 comments on commit fd86a92

Please sign in to comment.