nstudio / nativescript-plugins

@nstudio/nativescript-* plugin workspace.
Apache License 2.0
52 stars 40 forks source link

Callback on iOS returns no asset #42

Open swissdude opened 3 years ago

swissdude commented 3 years ago

CameraPlus

Set saveToGallery to false.

When photoCapturedEvent is emitted, the event.data contains not a saved image, but just an empty object.

Cause:

in index.ios.js Line 490 ff MySwifty.prototype.savePhoto

var asset_1 = new ImageAsset(this._photoToSave);

fails. this._photoToSave is an iOS UIImage and it seems ImageAsset can't handle this.

Solution

Safe the image and then return it.

So I added this to the function:

if (isIOS) {

  const imageFolder = knownFolders.documents(); 
  const iosImage = UIImageJPEGRepresentation(this._photoToSave, 0.7);

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

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

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

}

Of course, you can give the image a different name and save it at a different location.

PabloChiste commented 3 years ago

CameraPlus

Set saveToGallery to false.

When photoCapturedEvent is emitted, the event.data contains not a saved image, but just an empty object.

Cause:

in index.ios.js Line 490 ff MySwifty.prototype.savePhoto

var asset_1 = new ImageAsset(this._photoToSave);

fails. this._photoToSave is an iOS UIImage and it seems ImageAsset can't handle this.

Solution

Safe the image and then return it.

So I added this to the function:

if (isIOS) {

  const imageFolder = knownFolders.documents(); 
  const iosImage = UIImageJPEGRepresentation(this._photoToSave, 0.7);

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

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

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

}

Of course, you can give the image a different name and save it at a different location.

Can you show me how you left your code? I tried to add and it didn't work.

Did you also manage to decide on the selection of photos from the gallery? same problem occurs