Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS app for NS 7 crashes after user confirm photo #250

Open
andreasteffanoni opened this issue Sep 26, 2020 · 1 comment
Open

IOS app for NS 7 crashes after user confirm photo #250

andreasteffanoni opened this issue Sep 26, 2020 · 1 comment

Comments

@andreasteffanoni
Copy link

Environment
"nativescript": "^7.0.8",
"@nativescript/core": "^7.0.7",
"@nativescript/ios": "7.0.0",
"@nativescript/camera": "^5.0.0",

In a plain JS app, when taking a picture, the iOS version of the app crashes with the message " NativeScriptError: TypeError: Cannot read property 'creationDate' of undefined". The crash happens after taking the photo, when the user confirms the picture. The android version works without issues. The app was working fine before upgrading to NS 7.

To Reproduce

import { Application, ImageSource, fromObject, Utils } from '@nativescript/core';
import { isAvailable, requestPermissions, takePicture } from '@nativescript/camera';

var options = { width: 300, height: 300, keepAspectRatio: true, saveToGallery: true, cameraFacing: "rear", quickCapture: true }
takePicture(options)
    .then(function(imageAsset)
    {           
             // Code never reach this point
    }).catch(function (err) { console.log(err.message); });

Inspecting the camera plugin code, the error happens in line 54:

        var fetchResult = PHAsset.fetchAssetsWithOptions(fetchOptions);
        if (fetchResult.count > 0) {
               var asset = fetchResult[0];
               var dateDiff = asset.creationDate.valueOf() - currentDate_1.valueOf();   <---- line of error

Debugging, fetchResult.count is > 0 but asset is undefined (thus the error).

@andreasteffanoni
Copy link
Author

Still stuck with the issue. I had to bypass some code to make the plugin working. Here is my workaround.

                  if (this._saveToGallery) {
                       PHPhotoLibrary.sharedPhotoLibrary().performChangesCompletionHandler(function () {
                           PHAssetChangeRequest.creationRequestForAssetFromImage(imageSourceResult_1.ios);
                       }, function (success, err) {
                           /*
                           if (success) {                                
                               var fetchOptions = PHFetchOptions.alloc().init();
                               var sortDescriptors = NSArray.arrayWithObject(NSSortDescriptor.sortDescriptorWithKeyAscending("creationDate", false));
                               fetchOptions.sortDescriptors = sortDescriptors;
                               fetchOptions.predicate = NSPredicate.predicateWithFormatArgumentArray("mediaType = %d", NSArray.arrayWithObject(1));
                               var fetchResult = PHAsset.fetchAssetsWithOptions(fetchOptions);
                               if (fetchResult.count > 0) {
                                   var asset = fetchResult[0];
                                   var dateDiff = asset.creationDate.valueOf() - currentDate_1.valueOf();
                                   if (Math.abs(dateDiff) > 1000) {
                                       console.warn("Image asset returned was created more than 1 second ago");
                                   }
                                   imageAsset_1 = new core_1.ImageAsset(asset);
                                   _this.setImageAssetAndCallCallback(imageAsset_1);
                               }
                           }
                           else {
                               core_1.Trace.write("An error ocurred while saving image to gallery: " +
                                   err, core_1.Trace.categories.Error, core_1.Trace.messageType.error);
                           }*/
                       });
                   }
//                    else {
                       imageAsset_1 = new core_1.ImageAsset(imageSourceResult_1.ios);
                       this.setImageAssetAndCallCallback(imageAsset_1);
//                   }

This works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant