Using the camera in CrossMobile

81 views Asked by At

I am using CrossMobile to create an app and I want to use the camera to capture and save photos from my app. I will also need to access the photos taken from the app to show them in a list. How can I present the camera view on a button press?

1

There are 1 answers

0
Panayotis On

First of all you might need the CoreImage Plugin, or else some specific permissions will not be available.

Under iOS you also need to add the NSCameraUsageDescription key in the Info.plist by hand, (or else the application will crash due to Apple's limitation).

Let's assume that you have a UIButton with name cameraButton and an UIImageView with name imgV, both initialized in the loadView section of your code. Then the core would be similar to:

public void loadView() {
    // ....
    cameraButton.addTarget((sender, event) -> {
        if (!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
            new UIAlertView(null, "Unable to access camera", null, "Accept").show();
        else {
            UIImagePickerController picker = new UIImagePickerController();
            picker.setSourceType(UIImagePickerControllerSourceType.Camera);
            picker.setDelegate(new UIImagePickerControllerDelegate() {
                @Override
                public void didFinishPickingMediaWithInfo(UIImagePickerController picker, Map<String, Object> info) {
                    picker.dismissModalViewControllerAnimated(true);
                    UIImage img = (UIImage) info.get(UIImagePickerController.OriginalImage);
                    imgV.setImage(img);
                }

                @Override
                public void didCancel(UIImagePickerController picker) {
                    picker.dismissModalViewControllerAnimated(true);
                }
            });
            presentModalViewController(picker, true);
        }
    }, UIControlEvents.TouchUpInside);
    // ....
}