nstudio / nativescript-camera-plus

MIT License
79 stars 50 forks source link

Bug on iOS in savePhoto() #167

Open update-switzerland opened 2 years ago

update-switzerland commented 2 years ago

Using CameraPlus v. 4.1.0

There's still a bug in iOS that creates an error when sending a photo taken with the camera back to the calling function. Goal is to save the image in the file-system (not to the camera-roll)

this._owner.get().sendEvent(CameraPlus.photoCapturedEvent, asset);
this.resetPreview();

Sends back an asset that the calling script can't process

I had to change it like this:

this._owner.get().sendEvent(CameraPlus.photoCapturedEvent, this._photoToSave);
this.resetPreview();

Here's the callback in the calling script:

cam.on(CameraPlus.photoCapturedEvent, async (event) => {
        console.log('CameraPlus.photoCapturedEvent', event.data);

        try {

            // let image = event.data;

            if (isIOS) {
                const imageFolder = knownFolders.documents();

                console.log('event-data', event.data);

                const iosImage = UIImageJPEGRepresentation(event.data, 0.7);

                const result1 = NSFileManager.defaultManager.createFileAtPathContentsAttributes(imageFolder.path + "/cam_capture.jpg", iosImage, null);

                const asset = new ImageAsset(imageFolder.path + "/cam_capture.jpg");

                console.log('asset', asset);

                currentAsset = asset;

                viewModel.previewImage = asset;
                viewModel.didTakePicture = true;
                viewModel.canTakePicture = false;

                console.log('ios-image', asset);

            } 

        } catch (error) {
            console.error('error', error);
        }
    });