From 7f850b7923182b9c3ade5c7d556d4713882f62c1 Mon Sep 17 00:00:00 2001 From: Matt Saravitz Date: Fri, 22 Mar 2019 11:34:32 -0400 Subject: [PATCH 1/3] 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 --- src/camera.ios.ts | 12 ++++++++++-- src/index.d.ts | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/camera.ios.ts b/src/camera.ios.ts index c5edecf..c76f257 100644 --- a/src/camera.ios.ts +++ b/src/camera.ios.ts @@ -18,6 +18,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic private _height: number; private _keepAspectRatio: boolean; private _saveToGallery: boolean; + private _allowsEditing: boolean; public initWithCallback(callback: (result?) => void, errorCallback: (result?) => void): UIImagePickerControllerDelegateImpl { this._callback = callback; @@ -32,6 +33,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic this._width = options.width; this._height = options.height; this._saveToGallery = options.saveToGallery; + this._allowsEditing = options.allowsEditing; this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio; } return this; @@ -41,6 +43,9 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic if (info) { let currentDate: Date = new Date(); let source = info.valueForKey(UIImagePickerControllerOriginalImage); + if (this._allowsEditing) { + source = info.valueForKey(UIImagePickerControllerEditedImage); + } if (source) { let imageSource: typeof imageSourceModule = require("image-source"); let imageSourceResult = imageSource.fromNativeSource(source); @@ -129,11 +134,13 @@ export let takePicture = function (options): Promise { let reqHeight = 0; let keepAspectRatio = true; let saveToGallery = true; + let allowsEditing = false; if (options) { reqWidth = options.width || 0; reqHeight = options.height || reqWidth; keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? keepAspectRatio : options.keepAspectRatio; saveToGallery = types.isNullOrUndefined(options.saveToGallery) ? saveToGallery : options.saveToGallery; + allowsEditing = types.isNullOrUndefined(options.allowsEditing) ? allowsEditing : options.allowsEditing; } let authStatus = PHPhotoLibrary.authorizationStatus(); @@ -143,10 +150,10 @@ export let takePicture = function (options): Promise { if (reqWidth && reqHeight) { listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions( - resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery }); + resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery, allowsEditing: allowsEditing }); } else if (saveToGallery) { listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions( - resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio }); + resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio, allowsEditing: allowsEditing }); } else { listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve, reject); } @@ -162,6 +169,7 @@ export let takePicture = function (options): Promise { imagePickerController.sourceType = sourceType; imagePickerController.cameraDevice = options && options.cameraFacing === "front" ? UIImagePickerControllerCameraDevice.Front : UIImagePickerControllerCameraDevice.Rear; + imagePickerController.allowsEditing = allowsEditing; } imagePickerController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext; diff --git a/src/index.d.ts b/src/index.d.ts index f1397c5..e465d5b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -44,6 +44,12 @@ export interface CameraOptions { */ saveToGallery?: boolean; + /** + * iOS Only + * Defines if camera "Retake" or "Use Photo" screen forces user to crop camera picture to a square and optionally lets them zoom in. + */ + allowsEditing?: boolean; + /** * The initial camera. Default "rear". * The current implementation doesn't work on all Android devices, in which case it falls back to the default behavior. From 617b11cf3762d5d3e49755f2da1910a34ff4f33b Mon Sep 17 00:00:00 2001 From: Matt Saravitz Date: Fri, 22 Mar 2019 11:55:09 -0400 Subject: [PATCH 2/3] feat: add allowsEditing flag to demos --- demo-angular/app/app.component.html | 4 ++++ demo-angular/app/app.component.ts | 3 ++- demo-vue/app/components/Home.vue | 7 ++++++- demo/app/main-page.ts | 4 +++- demo/app/main-page.xml | 4 ++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/demo-angular/app/app.component.html b/demo-angular/app/app.component.html index 897bc53..acd318b 100644 --- a/demo-angular/app/app.component.html +++ b/demo-angular/app/app.component.html @@ -5,6 +5,10 @@ + + + + diff --git a/demo-angular/app/app.component.ts b/demo-angular/app/app.component.ts index 333e436..59860c9 100644 --- a/demo-angular/app/app.component.ts +++ b/demo-angular/app/app.component.ts @@ -8,6 +8,7 @@ import { ImageAsset } from 'tns-core-modules/image-asset'; }) export class AppComponent { public saveToGallery: boolean = false; + public allowsEditing: boolean = false; public keepAspectRatio: boolean = true; public width: number = 320; public height: number = 240; @@ -20,7 +21,7 @@ export class AppComponent { onTakePictureTap(args) { requestPermissions().then( () => { - takePicture({ width: this.width, height: this.height, keepAspectRatio: this.keepAspectRatio, saveToGallery: this.saveToGallery }) + takePicture({ width: this.width, height: this.height, keepAspectRatio: this.keepAspectRatio, saveToGallery: this.saveToGallery, allowsEditing: this.allowsEditing }) .then((imageAsset: any) => { this.cameraImage = imageAsset; let that = this; diff --git a/demo-vue/app/components/Home.vue b/demo-vue/app/components/Home.vue index 5cd90f8..d860775 100644 --- a/demo-vue/app/components/Home.vue +++ b/demo-vue/app/components/Home.vue @@ -10,6 +10,10 @@ + + + +