Nativescript: Camera module not working on IOS 10 (only in devices) / It hangs and exit the app

Created on 17 Sep 2016  路  35Comments  路  Source: NativeScript/NativeScript

Tell us about the problem

I detected the problem on my app and later verified on the sample-iOS-CameraApp
I submitted a PR with the necessary changes to run (https://github.com/NativeScript/sample-iOS-CameraApp/pull/15) but even that the application hangs at start.

Which platform(s) does your issue occur on?

iOS

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.3.0
  • Cross-platform modules: 2.3.0
  • Runtime(s): 2.3.0
  • Plugin(s): N/A

    Please tell us how to recreate the issue in as much detail as possible.

Just merge the PR and try to run the app on a device (Just confirmed it does not happen on the simulator, I tested on IPhone 6 / IOS 10.0.1).

bug done ios

All 35 comments

Guys some update on this ?

@enchev @dtopuzov guys some expected release date for this ?, is blocking my release, please notice the application crash when activating the camera module, it is not just it doesn't work.
Also I think it will happen in any updated IOS device (only tested in mine but seems to be related to the IOS version).

Will appreciate any feedback.

The basic example from the documentation also crashes iOS 10.

This is holding up my release too.

We are working actively on this and it should be live next week. @ignaciolarranaga, @3rror404 - is this OK for your release dates? Do we need to look for a workaround with the current implementation?

I was having this issue, adding:

<key>NSCameraUsageDescription</key>
<string>Some reason why you want to use the camera</string>

to my Info.plist fixed it for me.

Hi @valentinstoychev, I've moved testing until early next week and set expectations for the missing feature. It would be amazing if there was some sort of workaround available before then but no worries if this isn't possible.

@SpoonIsBad Adding the plist key allows the camera to open but tapping "Use photo" crashes the device. Did you experience this?

I'm actually using a QRCode module, so I don't have a "Use photo" option. I just noticed when I upgraded everything, the app was crashing whenever the QRCode module tried to use the camera, and adding the plist key fixed my problem.

I tried @SpoonIsBad 's suggestion, but no go. For me, my app doesn't crash, but cameraModule.takePicture() does nothing on a device. It does crash on the simulator.

I tried also @SpoonIsBad suggestion, it actually works for me (I take a picture and upload it to my servers without problems) but does not allow me to select pre-existing photos from the phone which I used to do in the past. It might be some change in the underlying API ? (seems it was that right?)

Guys just found this: http://useyourloaf.com/blog/privacy-settings-in-ios-10/

A significant change in iOS 10 is that you must declare ahead of time any access to private data or your App will crash. The fix is quick but easy to overlook if the usage is not a major feature of an App so here is your reminder if you are planning an iOS 10 migration.

If you are using one of these frameworks and fail to declare the usage your app will crash when it first makes the access. The crash log helpfully tells you which key you are missing.
For example, this is the result of accessing the camera without adding the key to Info.plist:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app鈥檚 Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

They keys: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

It seems the application hangs when moved to videos, but I wasn't able to found a particular key for videos (at least to prevent the application to hang, I'm not interested on videos in my app).

Hi all,

We've just finished (initial) work on native plugin for using device camera hardware. We have some issues to publish it to npm (there is a plugin with the same name :-(). We will try to get it - in case we succeed we will publish it like nativescript-camera. In worst case scenario we should change the name of the plugin which will not affect working code just some references. However you could use the current version of the plugin from its github repo. Within this repo there is an example how to use it (be aware that you will need latest tns-core-modules and tns-core-modules-widgets a.k.a. next. In order to produce package just run create.sh script from plugin root folder.

Let me know if you need some other assistance.

Nedyalko

Hi @nsndeck,

I had to chmod u+x compile.sh before the script would run.

$ chmod u+x compile.sh
$ bash create.sh

This gives me:

./compile.sh: line 21: node_modules/.bin/tsc: No such file or directory

No js files are created from the ts files.

Indeed script was created on a Windows machine (git bash console) and requires adding executable permissions for ios. Sorry my bad.

Hi @3rror404,

You should call npm install before create.sh in order to install all dependencies (typescript) is one of them. By the way I updated script files and write a small README.md that will help with plugin usage.

Nedyalko

Hi @nsndeck I download the repo, compile it an ran the application.
It indeed started and accessed the library but it crash at selecting a picture. Here is the log:

** BUILD SUCCEEDED **

Project successfully built
Successfully deployed on device with identifier '293C1CA2-BA5A-4DCE-B609-0742D89695A9'.
Oct  9 18:09:16 MacBook-Pro-de-Ignacio CameraTest[17095]: objc[17095]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x120656910) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x120480210). One of the two will be used. Which one is undefined.
Oct  9 18:09:16 MacBook-Pro-de-Ignacio CameraTest[17095]: assertion failed: 16A323 14A345: libxpc.dylib + 62597 [37A9DF49-35C1-3D93-B854-B35CACF0100F]: 0x7d
Oct  9 18:09:25 MacBook-Pro-de-Ignacio CameraTest[17095]: CONSOLE LOG file:///app/tns_modules/trace/trace.js:151:28: Debug: Application can access photo library assets.
Oct  9 18:09:29 MacBook-Pro-de-Ignacio CameraTest[17095]: [Generic] Creating an image format with an unknown type is an error
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 1   0x1097a8519 NativeScript::FFICallback<NativeScript::ObjCBlockCallback>::ffiClosureCallback(ffi_cif*, void*, void**, void*)
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 2   0x109e2b51e ffi_closure_unix64_inner
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 3   0x109e2bed2 ffi_closure_unix64
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 4   0x10d673810 _dispatch_call_block_and_release
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 5   0x10d69512e _dispatch_client_callout
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 6   0x10d67b24c _dispatch_queue_override_invoke
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 7   0x10d67ca0e _dispatch_root_queue_drain
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 8   0x10d67c7b4 _dispatch_worker_thread3
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 9   0x10da41746 _pthread_wqthread
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: 10  0x10da41221 start_wqthread
Oct  9 18:09:30 MacBook-Pro-de-Ignacio CameraTest[17095]: file:///app/tns_modules/nativescript-camera/nativescript-camera.js:69:55: JS ERROR TypeError: undefined is not an object (evaluating 'creationDate_1.valueOf')
Oct  9 18:09:30 MacBook-Pro-de-Ignacio com.apple.CoreSimulator.SimDevice.293C1CA2-BA5A-4DCE-B609-0742D89695A9.launchd_sim[16713] (UIKitApplication:org.nativescript.CameraTest[0x4cbf][17095]): Service exited due to Segmentation fault: 11

I tested on both IOS 9.1 and IOS 10 with the same results.

I see the same crash in the Simulator. Real device seems OK.

Hi @ignaciolarranaga, @3rror404

I've updated version of nativescript-camera plugin. It is now 0.0.3 and this issue is fixed.
I'm closing the issue (reopen if needed)

Nedyalko

Shouldn't this be open still since the docs refer to the older code?
https://docs.nativescript.org/hardware/camera

Ok maybe a new issue should be raised, but the docs definitely need updating, right?

@cfjedimaster I requested updated docs a few days ago https://github.com/NativeScript/docs/issues/530

Thanks!

Just to comment about the error on the simulator, it seems to happen only when requested to saveToGalery. Well it is kind of obvious since the problem is: "file:///app/tns_modules/nativescript-camera/nativescript-camera.js:69:55: JS ERROR TypeError: undefined is not an object (evaluating 'creationDate_1.valueOf')"

Seems an unprotected variable:

for (var i = 0; i < fetchResult.count; i++) {
    if (creationDate_1.valueOf() < fetchResult[i].creationDate.valueOf()) {
        var asset = fetchResult[i];
        imageAsset_1 = new imageAssetModule.ImageAsset(asset);
        break;
    }
}

@nsndeck how it is supposed an image to be saved to a file with the new plugin ?, I notice the image changed from ImageSource (which has .saveToFile(filePath, format)) to ImageAsset (which does not).

I think this is also related to @cfjedimaster / @3rror404 request for docs to be updated, since the old approach to save image seems not to be longer valid.

Why this change by the way ?

I just realized there is a way to create the ImageSource from the ImageAsset, sorry, my bad not to complete the research before post. Buy I this fromAsset should be static or a function, since this is kind of wear:

let source = new ImageSource();
source.fromAsset(this.image).then(
    imageSource => {
        ....
        if (imageSource.saveToFile(filePath, format)) {
            ....
        }
    }
}

Just an FYI, this (comment above) should be included in the docs. Basically - taking a picture and saving the image are, imo, pretty common things so it should all be shown.

I notice the image is also not resized as per the options given, i.e. the asset returned does not have the expected width/height in DIPs (nor in pixels). I validated this by logging once I get the imageSource (imageSource.width & imageSource.height).

Hi @ignaciolarranaga,

Nativescript-camera plugin resizes images to the screen size of the device. We added this in order to prevent some big bitmaps (it results in a out of memory issue on android). So if you ask for an image size bigger than screen size result image will be with the size of the screen. Indeed you and @cfjedimaster are absolutely right about the missing documentation, but we have some other things to do. We plan to extend this image-asset class to provide methods for saving image to file. So there will be no need to use image-source workaround.
About the non secured variable creationDate - I've made a small fix with version (0.0.4 of nativescript-camera plugin).

Nedyalko

Thanks @nsndeck, but I was actually meaning that before the resize was to the width/height specified, not the screen size. For example, I had a code like this:

let width = 1280 / platformModule.screen.mainScreen.scale;
let height = 960 / platformModule.screen.mainScreen.scale;
takePicture({ width: width, height: height, keepAspectRatio: false, saveToGallery: false })
    .then(picture => {
        let source = new ImageSource();
        source.fromAsset(picture)
            .then(imageSource => this.coverImage = imageSource);
        })
        .catch(error => console.log(error));

but it is no longer valid, since the requested width/height are not taken into account. Am I right ?
This is a little confusing because the CameraOptions does have width/height but it should have not if they are going to be ignored.

Custom width and height are definitely not working. I just tested with 300x300 and the image was saved at 2064x1161

@nsndeck, here is an example, I changed the code of the CameraTest in this way:

export function onTakePictureTap(args: EventData) {
    let page = <Page>(<View>args.object).page;
    let saveToGallery = page.bindingContext.get("saveToGallery");
    takePicture({width: 180, height: 180, keepAspectRatio: false, saveToGallery: saveToGallery}).
        then((imageAsset) => {
            let source = new ImageSource();
            source.fromAsset(imageAsset).then(source => {
                console.log("Size: " + source.width + "x" + source.height);
            });
            page.bindingContext.set("cameraImage", imageAsset);
        }, 
        (err) => {
            console.log("Error -> " + err.message);
        });
}

which should produce an square image, but says: CONSOLE LOG file:///app/main-page.js:25:24: Size: 375x667

and here is how it looks:
simulator screen shot oct 19 2016 10 41 03

Just raised an issue for tracking: https://github.com/NativeScript/nativescript-camera/issues/7

@ignaciolarranaga actually image-asset uses platformModule.screen.mainScreen.widthDIPS and heightDIPS to resize image. I've just saw a bug in the code of image-asset. I'll fix it and will test your scenario later today.

@nsndeck whenever you can please also check this issue: https://github.com/NativeScript/nativescript-camera/issues/10 something strange happens and the gallery does not open on the device. I noticed it happens even if you just request the permissions (the application hangs)

Hi @ignaciolarranaga,

You maybe already noticed that issue with custom image size has been resolved (completely for iOS and partly for Android). In order to resize the image on Android basically we have to create another bitmap (depending on size this could lead to Out-of-memory exception) therefore we postponed this a little bit to handle that scenario on native side (maybe immediately after the 2.4.0 release we will made necessary additions to tns-core-modules-widgets module).

Nedyalko

Thanks @nsndeck ! Are the changes included in nativescript-camera-0.0.4 ?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings