Skip to content

Commit

Permalink
Merge pull request #66 from aliyksel/master
Browse files Browse the repository at this point in the history
Handle user pressing cancel button for ios/ add Error for Android
  • Loading branch information
Dimitar Tachev committed Nov 3, 2017
2 parents 55cfe61 + 3aba531 commit 7c08d27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/camera.android.ts
Expand Up @@ -125,6 +125,9 @@ export let takePicture = function (options?): Promise<any> {
keepAspectRatio: shouldKeepAspectRatio
};
resolve(asset);
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
// User cancelled the image capture
reject(new Error("cancelled"));
}
});

Expand Down
14 changes: 9 additions & 5 deletions src/camera.ios.ts
Expand Up @@ -12,19 +12,22 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
}

private _callback: (result?) => void;
private _errorCallback: (result?) => void;

private _width: number;
private _height: number;
private _keepAspectRatio: boolean;
private _saveToGallery: boolean;

public initWithCallback(callback: (result?) => void): UIImagePickerControllerDelegateImpl {
public initWithCallback(callback: (result?) => void, errorCallback: (result?) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback;
this._errorCallback = errorCallback;
return this;
}

public initWithCallbackAndOptions(callback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
public initWithCallbackAndOptions(callback: (result?) => void, errorCallback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
this._callback = callback;
this._errorCallback = errorCallback;
if (options) {
this._width = options.width;
this._height = options.height;
Expand Down Expand Up @@ -117,6 +120,7 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
imagePickerControllerDidCancel(picker): void {
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
listener = null;
this._errorCallback(new Error("cancelled"));
}
}

Expand Down Expand Up @@ -144,13 +148,13 @@ export let takePicture = function (options): Promise<any> {

if (reqWidth && reqHeight) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
} else if (saveToGallery) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
resolve, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio });
resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio });
}
else {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve, reject);
}
imagePickerController.delegate = listener;

Expand Down

0 comments on commit 7c08d27

Please sign in to comment.