# How to convert Base64 into an Image

**URL:** <https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040>\
**Category:** ionic-v3\
**Created:** [January 12, 2018, 11:06am UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040 "2018-01-12T11:06:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![richiewijaya](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@richiewijaya](https://forum.ionicframework.com/u/richiewijaya)\
**Post date:** [January 12, 2018, 11:06am UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/1 "2018-01-12T11:06:18Z")

</div>

I have a Base64 (encoded from image), and I want to convert it again into an image in Ionic. Is it possible? and is there any example for do this?

Thanks before

---

<div class="post-metadata">

**Author:** ![RaghavendraKumar](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@RaghavendraKumar](https://forum.ionicframework.com/u/RaghavendraKumar)\
**Post date:** [January 12, 2018, 11:53am UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/2 "2018-01-12T11:53:00Z")

</div>

I guess you mean like using the image as ![]() - just replace base64Value with the base 64 data.

---

<div class="post-metadata">

**Author:** ![richiewijaya](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@richiewijaya](https://forum.ionicframework.com/u/richiewijaya)\
**Post date:** [January 12, 2018, 1:43pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/3 "2018-01-12T13:43:48Z")

</div>

Yes, do you have an example of that?

---

<div class="post-metadata">

**Author:** ![leonardoss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leonardoss/32/114499_2.png) [@leonardoss](https://forum.ionicframework.com/u/leonardoss)\
**Post date:** [January 12, 2018, 2:32pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/4 "2018-01-12T14:32:08Z")

</div>

You can create a blob from image an then show it in [src] bind, I created a service that work on blob

```auto
@Injectable()
export class ImageHandlerProvider {
    constructor () {
    }

    getBlob (b64Data) {
        contentType = '';
        sliceSize = 512;

        b64Data = b64Data.replace(/data\:image\/(jpeg|jpg|png)\;base64\,/gi, '');

        let byteCharacters = atob(b64Data);
        let byteArrays = [];

        for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
          let slice = byteCharacters.slice(offset, offset + sliceSize);
    
          let byteNumbers = new Array(slice.length);
          for (let i = 0; i < slice.length; i++) {
              byteNumbers[i] = slice.charCodeAt(i);
          }
    
          let byteArray = new Uint8Array(byteNumbers);
          byteArrays.push(byteArray);
        }

        let blob = new Blob(byteArrays, {type: contentType});
        return blob;
    }
}

```

and in you component you can use it like:

```auto
let urlCreator = window.URL || window.webkitURL;
let dataBlob = this.imageHandlerProvider.getBlob(data);
let imageUrl = urlCreator.createObjectURL(dataBlob);

this.imageUrl = imageUrl;

```

```auto
<img [src]="imageUrl" alt="Image" />

```

You can check [this codepen](https://codepen.io/leonardosalles/pen/goKpRd) for more details.

---

<div class="post-metadata">

**Author:** ![richiewijaya](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@richiewijaya](https://forum.ionicframework.com/u/richiewijaya)\
**Post date:** [January 14, 2018, 11:59am UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/5 "2018-01-14T11:59:05Z")

</div>

I tried the variable

```auto
var data = 'data:image/png;base64,iVBORw0KG...'

```

But it says

> Typescript Error: Unexpected token. A constructor, method, accessor, or property was expected.

And

```auto
var urlCreator = window.URL || window.webkitURL;

```

> Property ‘webkitURL’ does not exist on type ‘Window’.

Do you have any solution?

---

<div class="post-metadata">

**Author:** ![richiewijaya](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@richiewijaya](https://forum.ionicframework.com/u/richiewijaya)\
**Post date:** [January 15, 2018, 11:29am UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/6 "2018-01-15T11:29:14Z")

</div>

I changed the variable become

```auto
private data = '/9j/4AAQSkZJRgABAQIAHAAcAAD/7RmQUGhvdG9zaG9w... //base64

```

and I give a comment to the `window.webkitURL`

There is no error in my code, but the result is only “Image” caption, below the result

> ![Blob - Ionic lab](https://global.discourse-cdn.com/ionicframework/original/3X/9/5/95900f3830005b2dd9cadba62927fdfe78ab9c4b.png)

---

<div class="post-metadata">

**Author:** ![leonardoss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leonardoss/32/114499_2.png) [@leonardoss](https://forum.ionicframework.com/u/leonardoss)\
**Post date:** [January 15, 2018, 12:48pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/7 "2018-01-15T12:48:57Z")

</div>

In my case, I created a specific typescript file for base64:

```auto
export const SELFIE_PREVIEW_IMAGES = {
     image1: 'base64FromImage'
}

```

And then imported it on my component page

```auto
import { SELFIE_PREVIEW_IMAGES } from './selfie-preview.images';

images;
constructor () {
   this.images = SELFIE_PREVIEW_IMAGES;
}

```

And in html

```auto
<img [src]="images['image1']" alt="Image 1">

```

---

<div class="post-metadata">

**Author:** ![richiewijaya](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@richiewijaya](https://forum.ionicframework.com/u/richiewijaya)\
**Post date:** [January 15, 2018, 1:58pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/8 "2018-01-15T13:58:29Z")

</div>

In your case, when you use `getBlob()` function?  
May I know the full context?

---

<div class="post-metadata">

**Author:** ![leonardoss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leonardoss/32/114499_2.png) [@leonardoss](https://forum.ionicframework.com/u/leonardoss)\
**Post date:** [January 15, 2018, 2:13pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/9 "2018-01-15T14:13:32Z")

</div>

I use that for another reason, my server wants to receive a base64 but in device I get an image from file uri, so I need to convert this file url to base64 and blob to show it in page.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:55pm UTC](https://forum.ionicframework.com/t/how-to-convert-base64-into-an-image/118040/10 "2021-07-19T21:55:43Z")

</div>

Closed on July 19, 2021, 9:55pm UTC


