Error while saving image to gallery on ios

#1

Hi,

I have some problems when I want to save an image in the gallery in ios.
I tried differents methods but none works…

In my code the variable base64Data is a string which contain the base64 image (“data:image/png;base64,…”)

I tried with the base64ToGallery plugin:

this.base64ToGallery.base64ToGallery(base64Data, {prefix: 'img_', mediaScanner: false}).then(
			res => this.alertCtrl.create({title: res}).present(),
			err => this.alertCtrl.create({title: err}).present()
		);

But nothing happen with this code. If I set mediaScanner to true the app crash and same thing if I remove the option object (prefix and mediaScanner).

So I tried another solution:

public writeFile(base64Data: any, folderName: string, fileName: any) {
		let contentType = this.getContentType(base64Data);
		let DataBlob = this.base64toBlob(base64Data, contentType);
		// here iam mentioned this line this.file.externalRootDirectory is a native pre-defined file path storage. You can change a file path whatever pre-defined method.
		let filePath = this.file.externalRootDirectory + folderName;
		this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {
			this.alertCtrl.create({title: success}).present();
			console.log("File Writed Successfully", success);
		}).catch((err) => {
			this.alertCtrl.create({title: err.message}).present();
			console.log("Error Occured While Writing File", err);
		})
	}

	//here is the method used to get content type of an bas64 data
	public getContentType(base64Data: any) {
		let block = base64Data.split(";");
		let contentType = block[0].split(":")[1];
		return contentType;
	}

	//here is the method used to convert base64 data to blob data
	public base64toBlob(b64Data, contentType) {
		contentType = contentType || '';
		var sliceSize = 512;
		let byteCharacters = atob(b64Data.replace(/^data:image\/(png|jpeg|jpg);base64,/, ''));
		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);
			}
			var byteArray = new Uint8Array(byteNumbers);
			byteArrays.push(byteArray);
		}
		let blob = new Blob(byteArrays, {
			type: contentType
		});
		return blob;
	}

Here, this.file.externalRootDirectory is null and I have the error ENCODING_ERR.
So I saw that I can use “file:///” for ios and I got an NOT_FOUND_ERR, I suppose that this is not a good path but I don’t know how to get a good one.

Is there a way to know the good path or a code / plugin to save in the gallery that I didn’t find ?

Thanks for your help.