简体   繁体   English

react-native 获取 base64 图像的图像尺寸

[英]react-native get image dimensions of base64 image

I have the following code:我有以下代码:

let imgSrc =resolveAssetSource("data:image/png;base64,"+img); 
console.log(imgSrc);
let img = <Image source={{uri:"data:image/png;base64,"+img}} style={{width:imgSrc.width/2, height:imgSrc.height/2}} />;

But the code crashes because imgSrc is null.但是代码崩溃了,因为 imgSrc 为空。 How do I get the width and height of a base64 encoded image such that I can use it in my <Image style={} /> element?如何获取 base64 编码图像的宽度和高度,以便可以在我的<Image style={} />元素中使用它?

Get dimensions by the following way works for me.通过以下方式获取尺寸对我有用。

import { Image } from 'react-native';

Image.getSize(`data:image/png;base64,${img}`, (width, height) => {console.log(width, height)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM