flutter获取本地图片高度、宽度

2024-01-03 16:31:51
  /*获取本地图片宽度
  * */
  getLocalImageWidth(String path){
    int width;
    Completer<int> completer = new Completer<int>();
    Image image = Image.file(File.fromUri(Uri.parse(path)));
    // 预先获取图片信息
    image.image.resolve(new ImageConfiguration()).addListener(
        new ImageStreamListener((ImageInfo info, bool _) {
          width = info.image.width;
          print('height===$width');
          completer.complete(width);
        }));
    return completer.future;
  }

  /*获取本地图片高度
  * */
  getLocalImageHeight(String path){
    int height;
    Completer<int> completer = new Completer<int>();
    Image image = Image.file(File.fromUri(Uri.parse(path)));
    // 预先获取图片信息
    image.image.resolve(new ImageConfiguration()).addListener(
        new ImageStreamListener((ImageInfo info, bool _) {
          height = info.image.height;
          print('height===$height');
          completer.complete(height);
        }));
    return completer.future;
  }
//使用
int originalWidth,originalHeight;
originalWidth =  await Utils().getLocalImageWidth(imagePath);
originalHeight = await Utils().getLocalImageHeight(imagePath);

///======
//加载file类型的图片
return Image.file(imageFile,
           width: 200.w,
           height: 200.w,
           fit: BoxFit.fill,
           frameBuilder: (context, child, frame, wasSynchronouslyLoaded){
           		//加载完成,显示图片
               if(wasSynchronouslyLoaded){
                    return child;
                    }
                    //加载中,添加透明渐变
                    return AnimatedOpacity(
                            child: child,
                              opacity: frame == null ? 0 : 1,
                              duration: const Duration(seconds: 4),
                              curve: Curves.easeOut,
                       );
                       }),

//加载Uint8List类型的图片
return Image.memory(imageUint8List);
//如果得到的图片是base64加密的字符串,将字符串反编译出Uint8List数组后再加载
Image.memory(Base64Decoder().convert(slideImageBase64)));


文章来源:https://blog.csdn.net/androidhyf/article/details/135341143
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。