wymsee / cordova-imagePicker

Cordova Plugin For Multiple Image Selection
MIT License
407 stars 858 forks source link

Support base 64 image #14

Closed koolll closed 9 years ago

koolll commented 9 years ago

Hi, i love your plugin so much. But is this plugin support return base 64 instead of getting file path? btw hw to delete temporary image?

AdamMadrzejewski commented 9 years ago

I don't see base64 support, so I suppose it is not supported. Would be nice to have it, but if you need to convert URI to base64 there are some solutions on web to easily obtain it using cordova's file API.

But I think it would be a very time consuming process for multiple images.

allie-wake-up commented 9 years ago

No it doesn't. Base64 strings tend to result in memory issues so we avoid them. Is there some specific thing you can't do with the file path?

You can use the cordova/phonegap file plugin to copy/move/delete temporary files: https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

jakecraige commented 9 years ago

@koolll I have a fork that returns base64 encoded images on both ios and android instead of the file URI's

https://github.com/poetic/cordova-imagePicker

koolll commented 9 years ago

@jakecraige hi, thanks . i solve this issue by using canvas convert to base64. let me noe if need example

allie-wake-up commented 9 years ago

We're not going to add this going forward. We would consider a PR with this functionality added though.

atom2ueki commented 8 years ago

@koolll May I know how you use canvas convert to base64 in cordova-imagePicker?

kevincobain2000 commented 8 years ago

@jakecraige hi, thanks . i solve this issue by using canvas convert to base64. let me noe if need example

@koolll May I know how you use canvas convert to base64 in cordova-imagePicker?

What you need in your controller

    function convertImgToBase64URL(url, callback, outputFormat){
        var img = new Image();
        img.crossOrigin = 'Anonymous';
        img.onload = function(){
            var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'), dataURL;
            canvas.height = this.height;
            canvas.width = this.width;
            ctx.drawImage(this, 0, 0);
            dataURL = canvas.toDataURL(outputFormat);
            callback(dataURL);
            canvas = null; 
        };
        img.src = url;
    }

Using it

get the uri from plugin

      if (window.cordova) {
        window.imagePicker.getPictures(
            function (results) {
               for (var i = 0; i < results.length; i++) {
                   console.log('Image URI: ' + results[i]);
                    convertImgToBase64URL(uri, function(base64Img){
                        // Base64DataURL
                    });
               }
            }, function (error) {
            }, {
                maximumImagesCount: 10,
                width: 800
            }
        );
      };

Reference

// is the uri that you get from the plugin
var example_uri = "file:///var/mobile/Containers/Data/Application/56AF7EEA-C8FD-4E2D-BE6A-16BAAA29E87D/tmp/cdv_photo_014.jpg"
convertImgToBase64URL(example_uri, function(base64Img){
    // Base64DataURL
});