question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

selected.getImage is not a function

See original GitHub issue

I’m making an app where some data are stored in a SQL database. But in order to store images and videos I need to get the file uri first. I tried everything I could but nothing worked.

This is how my JS file looks like:

var imagepicker = require("nativescript-imagepicker");
var permissions = require( "nativescript-permissions" );
var ImageSourceModule = require("image-source");

function openImage() { 
   var context = imagepicker.create({ mode: "single" }); // use "multiple" for multiple selection
        context
    .authorize()
    .then(function() {
        return context.present();
    })
    .then(function(selection) {
        selection.forEach(function(selected) {
            selected.getImage().then((source) => {
                    console.log(selected.fileUri); 
                });     
        });  
    }).catch(function (e) {
        alert(e);// process error
    });
}


exports.openImage = openImage;

I can select images without a problem but then I get this error “selected.getImage is not a function”.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
linicommented, May 25, 2018

I tested this using the latest (6.0.1) version of the image picker plugin. For Android, getting the image path is very easy. Assuming selection is the array of image assets returned from the image picker:

selection.forEach(function(selected) {
     console.log(selected.android.toString()); 
});

For iOS, it is a bit more tricky as the native PHAsset object does not have a property containing the path:

selection.forEach(function(selected) {
    const ios = selected.ios;
    if (ios && ios.mediaType === PHAssetMediaType.Image) {
        const opt = PHImageRequestOptions.new();
        opt.version = PHImageRequestOptionsVersion.Current;
        PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
            ios, opt, (imageData: NSData, dataUTI: string, orientation: UIImageOrientation, info: NSDictionary<any, any>) => {
                console.log(info.objectForKey("PHImageFileURLKey").toString());
            });
    }
});
13reactions
linicommented, May 10, 2018

You need to add the tns-platform-declarations dependency to your app. It contains the definitions for all native objects like NSData and UIImageOrientation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nativescript imagepicker .getImage() is not a function error
I have been trying to implement the answer to this question but keep getting the error "selected.getImage is not a function".
Read more >
Uncaught TypeError: t.getLayerStatesArray is not a function ...
An Authentication object was not found in the SecurityContext. This request requires HTTP authentication. – csandreas1. Mar 13, 2019 at 12:30.
Read more >
GetImage Function - CSPro Help
The function returns a string containing the image filename, or a blank string if there is no value set image for the provided...
Read more >
CanvasRenderingContext2D.getImageData() - Web APIs | MDN
This method is not affected by the canvas's transformation matrix. ... The optional colorSpace setting allows you to get image data in the ......
Read more >
getimagesize - Manual
This function does not require the GD image library. ... Note: getimage size doesn't attempt to validate image file formats
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found