react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Base64 encoding/decoding

Open michalraska opened this issue 11 years ago • 25 comments

Hi, is there some module for base64 encoding/decoding which works well with react native? Thank you.

michalraska avatar May 05 '15 12:05 michalraska

If you just want to encode data on the JS side, you should just be able to pull in any 3rd party base64 module (here's one: https://github.com/dankogai/js-base64).

nicklockwood avatar May 05 '15 14:05 nicklockwood

@nicklockwood I use npm i js-base64 --save to include this to my react-native project.

but the redbox tells unable to resolve module from .../node_modules/js-base64/base64.js

xiekw2010 avatar Nov 19 '15 03:11 xiekw2010

This piece of code in js-base64 triggers the error:

  // if node.js, we use Buffer
  var buffer;
  if (typeof module !== 'undefined' && module.exports) {
    try {
      buffer = require('buffer').Buffer;
    } catch (err) {}
  }

The error doesn't get catched. Don't know if this is a bug of React.

starquake avatar Dec 07 '15 16:12 starquake

Buffer seems to be working: (just tested it) https://www.npmjs.com/package/buffer

See here on how to use it: http://www.hacksparrow.com/base64-encoding-decoding-in-node-js.html

I also successfully used this one: https://www.npmjs.com/package/base-64

starquake avatar Dec 07 '15 16:12 starquake

Hi, How do you get an image from the web ('i.e http://www.example.com/img.png') and convert it to base64 string on JS?

avishayil avatar Jan 20 '16 18:01 avishayil

@avishayil The short answer is that you probably shouldn't :-)

The longer answer is that you could do it on iOS as follows:

  1. Use Image.getSize(uri) to get the image dimensions.

  2. Use ImageEditor.cropImage(uri, cropData) to store a copy of the image in the ImageStore (if you pass the width and height you got in step 1) then cropImage won't actually crop the image, although it may still make a copy of it.

  3. Use ImageStore.getBase64ForTag(uri) to get the base64 data of the new image (pass the uri you got from the cropImage function, not the original).

  4. Don't forget to call ImageStore.removeImageForTag(uri) once you're done to delete the copy.

That may seem cumbersome, and it is, but that's because we generally don't encourage converting images to base64 data. If you can explain what you're trying to do, there might be a better approach, or we might be able to create a better API for it.

nicklockwood avatar Jan 20 '16 18:01 nicklockwood

Hi Nick, thanks for the answer. I'm building a reader app, and my current efforts are making article available for reading offline. Currently, i'm building the list of articles with the preview images. I'm using AsyncStorage to store all the article details offline, then fetch them when needed.

async handleAddFavourite(article) {
    AsyncStorage.getItem(FAV).then(function(value) {
      if (value !== null) {
        value = JSON.parse(value);
        index = value.map(function(e) { return e.id; }).indexOf(article.id);
        if (index > -1) {
          console.log('exists');
        } else {
          try {
            // article['base64_image'] = base64.encode(article.image); --> HERE THE BASE64 NEEDED
            value.push(article);
            AsyncStorage.setItem(FAV, JSON.stringify(value));
            console.log('Saved selection to disk: ' + value);
          } catch (error) {
            console.log('AsyncStorage error: ' + error.message);
          }
        }
      } else {
        value = [];
        try {
          value.push(article);
          AsyncStorage.setItem(FAV, JSON.stringify(value));
          console.log('Saved selection to disk: ' + value);
        } catch (error) {
          console.log('AsyncStorage error: ' + error.message);
        }
      }
    });
  };

One option that I had, is to do the base64 encoding on the server, but it turns out to be 'costly' operation for the API. That's why I'm trying to do it on the client side.

avishayil avatar Jan 20 '16 18:01 avishayil

@avishayil gotcha. The problem with that is obviously that you're sending the image data from native to JS (as base64 strings) and then back again via AsyncStorage, which is hugely wasteful.

The problem is that we don't currently have an API for saving uris directly to the file system on the native side without transferring the data to and from JS.

I've been thinking about ways we might be able to expose such an API. I'll keep doing so :-)

For your particular use case, your best solution right now would be to implement the archiving entirely on the native side with a custom module. But I can understand why you might not want to do that.

nicklockwood avatar Jan 20 '16 19:01 nicklockwood

@avishayil FWIW, we cache images to disk once they are downloaded, so you could probably get away with just saving the image url in asyncstorage (rather than the image data), because it should be possible to view the image url offline after the first time it's been loaded.

The only problem is that this will be slightly nondeterministic - the image cache size is limited, so the images may get evicted to make room, and we don't expose any controls for the cache size, etc.

nicklockwood avatar Jan 20 '16 19:01 nicklockwood

@nicklockwood How would I do that? currently i'm fetching the images using the Image component. Do I have a way to access the cached image afterwards?

avishayil avatar Jan 20 '16 19:01 avishayil

@avishayil you don't have to do anything special. The first time you load a uri in an <Image> it's downloaded and cached. The second time you display it, it's loaded from the cache, even if the app has been closed in the meantime.

nicklockwood avatar Jan 20 '16 19:01 nicklockwood

@nicklockwood Indeed, awesome! do you know if it's cached permanently (until user is clearing data, of course). Can I count on it for offline storage?

avishayil avatar Jan 20 '16 19:01 avishayil

@avishayil there are no guarantees I'm afraid. The OS is free to clear the cache if the device is low on space, and we limit the cache size (to 200MB IIRC), so if you download more than that, the new images will evict the old ones.

nicklockwood avatar Jan 20 '16 20:01 nicklockwood

I can't believe it would come to that sizes, but it's good to know. Anyway, if you have a better, stable solution i'd be glad to know about it.

avishayil avatar Jan 20 '16 20:01 avishayil

@nicklockwood Thanks for telling us how to use ImageEditor.cropImage!

I am using it to resize images before uploading them to the server. However, I noticed that the uploaded images are saved in PNG format, not JPEG format as the original image.

It seems to have something to do with cropped/resized image getting alpha channel which causes later logic to think it should be in PNG format.

daesan avatar Feb 23 '16 19:02 daesan

@daesan ah, good point. I know why that's happening - I'll put up a fix.

nicklockwood avatar Feb 23 '16 23:02 nicklockwood

@nicklockwood I got exactly the same exact use case here trying to save images locally for offline viewing. Correct me if I am wrong but it seems to me that even if I save images with native module to local file system I still won't be able to render them with <Image>

lananelson avatar Mar 16 '16 19:03 lananelson

@Iananelson wrong; as long as you can have a uri for the image, you can display it. That works with file:// type uris as well as remote ones, and you can use relative uris for files saved locally, e.g:

<Image source={{uri: '~/Library/Caches/someimage.png'}}/>

nicklockwood avatar Mar 16 '16 19:03 nicklockwood

oh thats great! thanks @nicklockwood

lananelson avatar Mar 18 '16 15:03 lananelson

@daesan FYI, the alpha/png issue should be fixed now.

nicklockwood avatar Mar 18 '16 15:03 nicklockwood