diff --git a/BCL.csproj b/BCL.csproj index 2cbbba984..f2a36aea9 100644 --- a/BCL.csproj +++ b/BCL.csproj @@ -218,6 +218,9 @@ + + camera.d.ts + diff --git a/Tests/image-source-tests.ts b/Tests/image-source-tests.ts index d22c41975..c5932f7ec 100644 --- a/Tests/image-source-tests.ts +++ b/Tests/image-source-tests.ts @@ -13,15 +13,14 @@ // import imageSource = require("image-source/image-source"); -import app = require("application/application"); import fs = require("file-system/file-system"); +import app = require("application/application"); import TKUnit = require("Tests/TKUnit"); - export var testFromResource = function () { // // ### Load image using resource name - // this similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS + // This is similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS // ``` JavaScript var img = imageSource.fromResource("logo"); // ``` diff --git a/camera/camera-types.ts b/camera/camera-types.ts new file mode 100644 index 000000000..e82bd6f4d --- /dev/null +++ b/camera/camera-types.ts @@ -0,0 +1,23 @@ + +export enum CameraPosition { + FRONT = 0, + BACK = 1, +} + +export enum FlashMode { + AUTO = 0, // default + ON = 1, + OFF = 2 +} + +export interface Options { + /** + * Specifies which Camera to use + */ + cameraPosition?: CameraPosition; + + /** + * Specifies flash mode + */ + flashMode?: FlashMode; +} diff --git a/camera/camera.d.ts b/camera/camera.d.ts index b14ceec9f..e26c7c9ca 100644 --- a/camera/camera.d.ts +++ b/camera/camera.d.ts @@ -1,4 +1,8 @@ -export declare enum CameraPosition { + +import promises = require("promises/promises"); +import imageSource = require("image-source/image-source"); + +export declare enum CameraPosition { FRONT = 0, BACK = 1, } @@ -9,10 +13,24 @@ export declare enum FlashMode { OFF = 2 } +export interface Options { + /** + * Specifies which Camera to use + */ + cameraPosition?: CameraPosition; + + /** + * Specifies flash mode + */ + flashMode?: FlashMode; +} + // TODO most of hardware related parts need to handle onPause and onResume of the calling activities export declare class CameraManager { takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any); // options { useSavedPhotos: true } pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any); -} \ No newline at end of file +} + +export declare var takePicture: (options?: Options) => promises.Promise; diff --git a/camera/camera.ios.ts b/camera/camera.ios.ts index f31b25845..635063a86 100644 --- a/camera/camera.ios.ts +++ b/camera/camera.ios.ts @@ -1,5 +1,9 @@ -var REQUEST_IMAGE_CAPTURE: number = 1; -var REQUEST_SELECT_PICTURE: number = 2; + +import promises = require("promises/promises"); +import imageSource = require("image-source/image-source"); +import types = require("camera/camera-types"); + +var imagePickerController; export class CameraManager { public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) { @@ -8,4 +12,68 @@ export class CameraManager { // options { useSavedPhotos: true } public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) { } -} \ No newline at end of file +} + +function topViewController():UIKit.UIViewController { + return topViewControllerWithRootViewController(UIKit.UIApplication.sharedApplication().keyWindow.rootViewController); +} + +function topViewControllerWithRootViewController(rootViewController: any) { + if (rootViewController.isKindOfClass(UIKit.UITabBarController.class())) { +// console.log("Using tab view controller"); + return topViewControllerWithRootViewController(rootViewController.selectedViewController); + } else if (rootViewController.isKindOfClass(UIKit.UINavigationController.class())) { +// console.log("Using navigation view controller"); + return topViewControllerWithRootViewController(rootViewController.visibleViewController); + } else if (rootViewController.presentedViewController) { +// console.log("Using presented view controller"); + return topViewControllerWithRootViewController(rootViewController.presentedViewController); + } else { +// console.log("Using view controller"); + return rootViewController; + } +} + +export var takePicture = function (options?: types.Options): promises.Promise { + var d = promises.defer(); + + // FIXME: this is done to prevent listener from being freed by JS garbage collector + // we will try to fix this at JS Bridge level + var listener; + + var ImagePickerControllerListener = Foundation.NSObject.extends({ + }, {}).implements({ + + protocol: "UIImagePickerControllerDelegate", + + implementation: { + imagePickerControllerDidFinishPickingMediaWithInfo: function (picker, info) { + console.log('takeImage received'); + picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null); + // FIXME: do not work with listener here + listener = null; + var image = imageSource.fromNativeSource(info.valueForKey(UIKit.UIImagePickerControllerOriginalImage)); + d.resolve(image); + }, + + imagePickerControllerDidCancel: function (picker) { + console.info('takeImage canceled'); + picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null); + // FIXME: do not work with listener here + listener = null; + d.reject(new Error('takePicture canceled by user')); + } + } + }); + + imagePickerController = new UIKit.UIImagePickerController(); + listener = new ImagePickerControllerListener(); + imagePickerController.delegate = listener; + imagePickerController.mediaTypes = UIKit.UIImagePickerController.availableMediaTypesForSourceType(UIKit.UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera); + imagePickerController.sourceType = UIKit.UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera; + imagePickerController.modalPresentationStyle = UIKit.UIModalPresentationStyle.UIModalPresentationCurrentContext; + + topViewController().presentViewControllerAnimatedCompletion(imagePickerController, true, null); + + return d.promise(); +}