rodionovd
I obtained the entire document and recursively traversed each layer, where the layer represents each layer
My other UI document using this code is valid, but this document will prompt an error. Is it due to document settings
// 数据转换
switch (layer.type) {
case 'Image':
// console.log('image')
// 获取图像的base64表示
const imageBase64 = layer.image.nsdata.base64EncodedStringWithOptions(0);
// 将imageBase64属性添加到图像图层中
copy[i].src = 'data:image/png;base64,${imageBase64}';
break
case 'ShapePath':
case 'Shape':
// console.log('shape')
try {
let svgBuffer = sketch.export(layer, {formats: 'svg', output: false, compact: true, overwriting: true})
copy[i].src = 'data:image/svg+xml;base64,${Buffer.from(svgBuffer).toString('base64')}'
} catch (e) {
console.log('shape导出失败', e)
copy[i].src = ''
}
// 获取整个层导出图后删除layer
copy[i].layers = []
break
case 'Slice':
// console.log('slice')
let img = ''
try {
img = sketch.export(layer, {formats: 'png', output: false, includeBackgroundColor: true, includeArtboardBackground: true})
} catch (e) {
console.log(e, layer)
break
}
copy[i].src = 'data:image/png;base64,${Buffer.from(img).toString('base64')}'
// 切片类型获取到切片图片后 删除相邻layer只留切片
copy.splice(0, i)
copy.splice(1)
return;
}