For high-performance requirements (e.g., capturing views in < 16ms), use the raw format and zip-base64 result type on Android. This avoids expensive compression during the capture process.
Workflow for Android:
- Set
format: 'raw' and result: 'zip-base64'. - The returned data follows the pattern
width:height|base64_string. - Extract dimensions and the base64 string.
- Use
zlib.inflateSync to decompress the data. - Use
pngjs to convert the inflated buffer into a PNG.
Note: Packaging PNG data is CPU intensive; consider using process.fork() for the conversion logic.
// Required packages: npm install pngjs zlib
const fs = require("fs");
const zlib = require("zlib");
const PNG = require("pngjs").PNG;
const Buffer = require("buffer").Buffer;
const format = Platform.OS === "android" ? "raw" : "png";
const result = Platform.OS === "android" ? "zip-base64" : "base64";
captureRef(this.ref, {result, format}).then(data => {
// expected pattern 'width:height|', example: '1080:1731|'
const resolution = /^(\d+):(\d+)\|/g.exec(data);
const width = (resolution || ["", 0, 0])[1];
const height = (resolution || ["", 0, 0])[2];
const base64 = data.substr((resolution || [""])[0].length || 0);
// convert from base64 to Buffer
const buffer = Buffer.from(base64, "base64");
// un-compress data
const inflated = zlib.inflateSync(buffer);
// compose PNG
const png = new PNG({width, height});
png.data = inflated;
const pngData = PNG.sync.write(png);
// save composed PNG
fs.writeFileSync(output, pngData);
});