When updating an old React Native application I ran into a problem with legacy code. It uses ImageStore
addImageFromBase64()
in one method to:
How should I go about modifying it to achieve the same desired functionality?
The problem is that addImageFromBase64()
doesn't work on Android.
Warning: react-native: ImageStore.addImageFromBase64() is not implemented on android
// Resize photo (fill white space on top and bottom)
static async addWhiteSpace(data) {
const {
height,
width,
base64Str
} = data;
const cropData = {
offset: {
x: 0,
y: - height / 2,
},
size: {
width,
height: 2 * height
},
resizeMode: 'contain'
};
const uriResolver = await new Promise((resolve, reject) => {
ImageStore.addImageFromBase64(base64Str, (uri) => {
resolve(uri)
}, (err) => {
reject(err)
})
});
const whitespaceURIResolver = await new Promise((resolve, reject) => {
ImageEditor.cropImage(uriResolver, cropData, (whitespaceURI) => {
resolve(whitespaceURI)
}, (err) => {
reject(err)
})
});
const base64Resolver = await new Promise((resolve, reject) => {
ImageStore.getBase64ForTag(whitespaceURIResolver, (base64) => {
resolve(base64)
}, (err) => {
reject(err)
})
});
return base64Resolver
}
This solved the problem, now it's running on both Android and iOS:
static async addWhiteSpace(data) {
const {
height,
width,
base64Str
} = data;
const cropData = {
offset: {
x: 0,
y: Math.abs(- height / 2),
},
size: {
width,
height: 2 * height
},
resizeMode: 'contain'
};
const uriResolver = await RNFS.writeFile(RNFS.CachesDirectoryPath + "temp.png", base64Str, 'base64');
const whitespaceURIResolver = await new Promise((resolve, reject) => {
ImageEditor.cropImage("file:" + uriResolver, cropData, (whitespaceURI) => {
resolve(whitespaceURI)
}, (err) => {
reject(err)
})
});
const base64Resolver = await RNFS.readFile(whitespaceURIResolver, "base64");
return base64Resolver
}