Skip to content

Commit 7f850b7

Browse files
committed
feat: allowsEditing on iOS
Add allowsEditing flag for iOS. This flag forces square crop and allows for zoom after photo has been taken. Issue #180
1 parent 8e32961 commit 7f850b7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/camera.ios.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
1818
private _height: number;
1919
private _keepAspectRatio: boolean;
2020
private _saveToGallery: boolean;
21+
private _allowsEditing: boolean;
2122

2223
public initWithCallback(callback: (result?) => void, errorCallback: (result?) => void): UIImagePickerControllerDelegateImpl {
2324
this._callback = callback;
@@ -32,6 +33,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
3233
this._width = options.width;
3334
this._height = options.height;
3435
this._saveToGallery = options.saveToGallery;
36+
this._allowsEditing = options.allowsEditing;
3537
this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
3638
}
3739
return this;
@@ -41,6 +43,9 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
4143
if (info) {
4244
let currentDate: Date = new Date();
4345
let source = info.valueForKey(UIImagePickerControllerOriginalImage);
46+
if (this._allowsEditing) {
47+
source = info.valueForKey(UIImagePickerControllerEditedImage);
48+
}
4449
if (source) {
4550
let imageSource: typeof imageSourceModule = require("image-source");
4651
let imageSourceResult = imageSource.fromNativeSource(source);
@@ -129,11 +134,13 @@ export let takePicture = function (options): Promise<any> {
129134
let reqHeight = 0;
130135
let keepAspectRatio = true;
131136
let saveToGallery = true;
137+
let allowsEditing = false;
132138
if (options) {
133139
reqWidth = options.width || 0;
134140
reqHeight = options.height || reqWidth;
135141
keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? keepAspectRatio : options.keepAspectRatio;
136142
saveToGallery = types.isNullOrUndefined(options.saveToGallery) ? saveToGallery : options.saveToGallery;
143+
allowsEditing = types.isNullOrUndefined(options.allowsEditing) ? allowsEditing : options.allowsEditing;
137144
}
138145

139146
let authStatus = PHPhotoLibrary.authorizationStatus();
@@ -143,10 +150,10 @@ export let takePicture = function (options): Promise<any> {
143150

144151
if (reqWidth && reqHeight) {
145152
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
146-
resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
153+
resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery, allowsEditing: allowsEditing });
147154
} else if (saveToGallery) {
148155
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
149-
resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio });
156+
resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio, allowsEditing: allowsEditing });
150157
} else {
151158
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve, reject);
152159
}
@@ -162,6 +169,7 @@ export let takePicture = function (options): Promise<any> {
162169
imagePickerController.sourceType = sourceType;
163170
imagePickerController.cameraDevice = options && options.cameraFacing === "front" ?
164171
UIImagePickerControllerCameraDevice.Front : UIImagePickerControllerCameraDevice.Rear;
172+
imagePickerController.allowsEditing = allowsEditing;
165173
}
166174

167175
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext;

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface CameraOptions {
4444
*/
4545
saveToGallery?: boolean;
4646

47+
/**
48+
* iOS Only
49+
* Defines if camera "Retake" or "Use Photo" screen forces user to crop camera picture to a square and optionally lets them zoom in.
50+
*/
51+
allowsEditing?: boolean;
52+
4753
/**
4854
* The initial camera. Default "rear".
4955
* The current implementation doesn't work on all Android devices, in which case it falls back to the default behavior.

0 commit comments

Comments
 (0)