NativeScript / nativescript-camera

NativeScript plugin to empower using device camera.
Apache License 2.0
92 stars 47 forks source link

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

Open andreasteffanoni opened 3 years ago

andreasteffanoni commented 3 years ago

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 commented 3 years ago

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.