I have a problem converting local image inside my angular project folder to base64 and displaying it. I have this structure: src > app > screenA > components > gallery > mock > images (here I store images image_1.jpg, image_2.jpg ...) I then have this structure: src > app > screenA > components > gallery > mock > mock_images.ts:
import {Image} from '....';
export const mockImages: Image[] = [
{
imageSrc: 'image_1.jpg',
imageDesc:'Description of the first picture',
},
{
imageSrc: 'image_2.jpg',
imageDesc: 'Description of the second picture.',
},
{
imageSrc: 'image_3.jpg',
imageDesc: 'Description of the third picture.',
},
{
imageSrc: 'image_4.jpg',
imageDesc: 'Description of the fourth picture.',
},
{
imageSrc: 'image_5.jpg',
imageDesc: 'Description of the fifth picture.',
},
{
imageSrc: 'image_6.jpg',
imageDesc: 'Description of the sixth picture.',
},
];
Then, inside my service I define:
mockImagesPath = '.mock/images';
mockImages: Image[] = JSON.parse(JSON.stringify(mockImages));
data: Image[] = [];
My html:
<div *ngFor="let img of imgs">
<img [src]="img.imageSrc" />
</div>
In the ngOnInit I will call the service method and then fill the imgs with data coming from service:
convertImg(): void {
const numberOfFiles = this.mockImages.length;
for (let i = 0; i < numberOfFiles; i++) {
const reader = new FileReader();
const url = reader.readAsDataURL(`${this.mockImagesPath}/${this.mockImages[i].imageSrc}`);
this.data.push(new Image(url, this.mockImages[i].imageDesc));
}
}
Although this doesn´t work. Can you suggest some fix?