0

I have the following code created by a friend on Android using Java.

private void carregaFotos(Long idResenhaCurrent, Long idResenhaGedave) {
    String root = Environment.getExternalStorageDirectory().toString();
    Resources res = getResources();
    File myDir = new File(root + "/" + res.getString(R.string.app_name_files_res) + "/Imagem");
    ResenhaEqFoto foto = null;
    List<ResenhaEquideoFotos> fotosList = new ArrayList<>();
    fotosList = equideoFotosUtil.getListDados(equideoDados.getId());
    int qtde = fotosList.size();
    for (int i = 0; i < qtde; i++) {
            foto = new ResenhaEqFoto();
            String path = myDir+"/"+fotosList.get(i).getNomeArquivo().trim();
            Bitmap bm = BitmapFactory.decodeFile(path);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 70, baos); // bm is the bitmap object  era quality 100
            byte[] b = baos.toByteArray();

            foto.setImagem( Base64.encode(b, 0) );

// more data added to the "foto" object
...
            dadosFotoList.add( foto );

    }
  }

The code get an image that are stored inside the app project folder, get the file and transform it into a Bitmap, after that transform it in a ByteArrayOutputStream and compress the file.

After that encode the file into Base64 String.

This is what I could understand of it.

First of all, what exactly is

byte[] b

in Swift and how can I use the code below

Base64.encode(b, 0) 

with Swift?

The code above generate the following JSON, take a look in the link below:

https://jsonblob.com/a260a537-da9f-11eb-b281-0b0ddf298faa

The part that I want to achieve is the "imagem" part.

A array of ints(or bytes).

How can I achieve this result?

fabiobh
  • 705
  • 2
  • 13
  • 33
  • No, your link convert an UIImage to a base64 String. The code that I post here is encoding a byte array, not a String. – fabiobh Jul 01 '21 at 20:17
  • The link that I provided shows how to get `Data` or `NSData` from a UIImage, which *is* essentially a byte array. – jnpdx Jul 01 '21 at 20:32
  • I use the print command on Xcode to see the String representation of the NSData. It is very different from the byte[] String representation. byte[] is something like "[47,57,106,83,...] and the String representation of NSData is "uAABADD8QfGX+z8wQH" – fabiobh Jul 01 '21 at 20:48
  • 1
    Those are two very different formats of displaying the data -- it doesn't mean they aren't the same. However, there's no guarantee that the conversion to PNG or JPEG on iOS/Android would be the same either -- especially with JPEG where there's compression. – jnpdx Jul 01 '21 at 21:09

0 Answers0