Skip to content

selected.getImage is not a function #181

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

Closed
pierreamgabriel opened this issue Apr 21, 2018 · 15 comments
Closed

selected.getImage is not a function #181

pierreamgabriel opened this issue Apr 21, 2018 · 15 comments

Comments

@pierreamgabriel
Copy link

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".

@kvindascr
Copy link

Pierre im facing a similar issue, well, not exactly, but on the latest version of the plugin v6, the selection is really an ImageAsset from nativescript and it is not exactly working as the code you indicate.
That should work for version 5 and before, but Im also still in the process to understand how it is supposed to work on the latest version, because I see no fileUri or similar properties.

@lini lini added the question label Apr 26, 2018
@lini
Copy link
Contributor

lini commented Apr 26, 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());
            });
    }
});

@kvindascr
Copy link

Great info @lini thanks for sharing.

@bachras
Copy link

bachras commented May 9, 2018

@lini thank you for sharing your solution. Can you provide whole component code? As it says it cannot find NSData, UIImageOrientation, NSDictionary . I try to declare them but it didn't help. I haven't worked with native iOS code before. Thank you in advance.

@lini
Copy link
Contributor

lini commented 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.

@bachras
Copy link

bachras commented May 10, 2018

thanks @lini

@sivamamidi-REISys
Copy link

+1

@sivamamidi-REISys
Copy link

Please provide good documentation for last two versions.

@ppulwey
Copy link

ppulwey commented Oct 1, 2018

Sorry, to start the question here again, but how can I get the path to a Video?
I've tried to figure it out by myself, but I stuck. What I have so far:

imagepicker.create({
          maximumNumberOfSelection: 1,
          minimumNumberOfSelection: 1,
          mediaType: imagepicker.ImagePickerMediaType.Video,
          mode: 'single',
          prompt: 'Bitte Video auswählen'
        }).present()
          .then(
            (videoAsset: any) => {
              const selected = videoAsset[0];
              const ios = selected.ios;

              if (ios) {
                const opt = PHVideoRequestOptions.new();
                opt.version = PHVideoRequestOptionsVersion.Current;

                PHImageManager.defaultManager().requestAVAssetForVideoOptionsResultHandler(
                  ios, opt, (asset: AVAsset, audioMix: AVAudioMix, info: NSDictionary<any, any>) => {
                    console.log(info.objectForKey("PHImageFileURLKey").toString());
                  });
              }
            },
            (rej) => {
              console.log(rej);
            });

I have the Video Player plugin installed and want to play the selected video in the app as a preview and later upload it to firebase.

But I can't get this thing working. Any ideas / solutions?

@ehibes
Copy link

ehibes commented Oct 12, 2018

@ppulwey this works for me :

const selected = selection[0];
const ios = selected.ios;

if (ios) {
  const opt = PHVideoRequestOptions.new();
  opt.version = PHVideoRequestOptionsVersion.Current;
  PHImageManager.defaultManager().requestAVAssetForVideoOptionsResultHandler(
    ios, opt, (asset: AVAsset, audioMix: AVAudioMix, info: NSDictionary<any, any>) => {
      let regex = /(file[^>]*)/g
      let file = asset.toString().match(regex)[0];
      console.log(file);
    });
}

@falodunos
Copy link

@pierremacedo find my complete solution here ...

https://github.com/NativeScript/nativescript-imagepicker/issues/197#issuecomment-431783669

@ibnYusrat
Copy link

How do we copy the image to app's directory to be able to actually make some use of that path?

@keerl
Copy link

keerl commented Oct 6, 2019

This no longer works on iOS 13. PHImageFileURLKey has been deprecated. https://stackoverflow.com/questions/57202965/phimageresultisdegradedkey-phimagefileurlkey-is-not-found

@uzarsalan
Copy link

uzarsalan commented Oct 29, 2019

Working solution

const ios: PHAsset = selected.ios;
if (ios && ios.mediaType === PHAssetMediaType.Image) {
  filePath = await new Promise((resolve, reject) => {
    ios.requestContentEditingInputWithOptionsCompletionHandler(
      null,
      (input, info) => {
        resolve(input.fullSizeImageURL.toString().replace("file://", ""));
      }
    );
  });
}

@dmoptimal
Copy link

@uzarsalan I'm using your example and it works occasionally for me, but quite often (especially with older photos) I get this error when selecting an image:

TypeError: null is not an object (evaluating 'input.fullSizeImageURL')

I logged the info & input params inside the function and I see this:

info:

{
    PHContentEditingInputErrorKey = "Error Domain=PHPhotosErrorDomain Code=3164 \"(null)\"";
    PHContentEditingInputResultIsInCloudKey = 1;
    PHImageResultRequestIDKey = 21;
}

input:
null

Does anyone know what this issue is? I'm guessing its something to do with iCloud maybe? as in, photos backing up to iCloud, then no longer being "on device". I'm new to this so not exactly sure.

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