nativescript-camera icon indicating copy to clipboard operation
nativescript-camera copied to clipboard

IOS app for NS 7 crashes after user confirm photo

Open andreasteffanoni opened this issue 5 years ago • 1 comments

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 avatar Sep 26 '20 17:09 andreasteffanoni

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.

andreasteffanoni avatar Sep 29 '20 08:09 andreasteffanoni