Flutter获取图片大小(网络图片和本地图片)

WechatIMG32.png

可以获取图片的大小

使用

本地资源使用:

  AsperctRaioImage.asset(
            'images/img_home_bg.jpg',
            builder: (context, snapshot, url) {
              print('width=${snapshot.data.width}');
              print('heiht=${snapshot.data.height}');
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('本地资源加载',style: TextStyle(fontSize: 25.0,color: Colors.black),),
                  Text('大小--${snapshot.data.width.toDouble()}x${snapshot.data.height.toDouble()}',style: TextStyle(fontSize: 17.0),),
                  Container(
                    width: snapshot.data.width.toDouble() / 5,
                    height: snapshot.data.height.toDouble() / 5,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                          image: AssetImage(url), fit: BoxFit.cover),
                    ),
                  )
                ],
              );
            },),

网络资源使用:

AsperctRaioImage.network(
              'http://g.hiphotos.baidu.com/image/pic/item/c2cec3fdfc03924590b2a9b58d94a4c27d1e2500.jpg',
              builder: (context, snapshot, url) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('网络图片加载',style: TextStyle(fontSize: 25.0,color: Colors.black),),
                    Text('大小--${snapshot.data.width.toDouble()}x${snapshot.data.height.toDouble()}',style: TextStyle(fontSize: 17.0),),
                    Container(
                      width: snapshot.data.width.toDouble() / 5,
                      height: snapshot.data.height.toDouble() / 5,
                      decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage(url), fit: BoxFit.cover),
                      ),
                    )
                  ],
                );
              })

AsperctRaioImage工具类

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

typedef AsyncImageWidgetBuilder<T> = Widget Function(
    BuildContext context, AsyncSnapshot<T> snapshot, String url);

typedef AsyncImageFileWidgetBuilder<T> = Widget Function(
    BuildContext context, AsyncSnapshot<T> snapshot, File file);

typedef AsyncImageMemoryWidgetBuilder<T> = Widget Function(
    BuildContext context, AsyncSnapshot<T> snapshot, Uint8List bytes);

enum AsperctRaioImageType { NETWORK, FILE, ASSET, MEMORY }

///有宽高的Image
class AsperctRaioImage extends StatelessWidget {
  String url;
  File file;
  Uint8List bytes;
  final ImageProvider provider;
  AsperctRaioImageType type;
  AsyncImageWidgetBuilder<ui.Image> builder;
  AsyncImageFileWidgetBuilder<ui.Image> filebBuilder;
  AsyncImageMemoryWidgetBuilder<ui.Image> memoryBuilder;

  AsperctRaioImage.network(url, {Key key, @required this.builder})
      : provider = NetworkImage(url),
        type = AsperctRaioImageType.NETWORK,
        this.url = url;

  AsperctRaioImage.file(
    file, {
    Key key,
    @required this.filebBuilder,
  })  : provider = FileImage(file),
        type = AsperctRaioImageType.FILE,
        this.file = file;

  AsperctRaioImage.asset(name, {Key key, @required this.builder})
      : provider = AssetImage(name),
        type = AsperctRaioImageType.ASSET,
        this.url = name;

  AsperctRaioImage.memory(bytes, {Key key, @required this.memoryBuilder})
      : provider = MemoryImage(bytes),
        type = AsperctRaioImageType.MEMORY,
        this.bytes = bytes;

  @override
  Widget build(BuildContext context) {
    final ImageConfiguration config = createLocalImageConfiguration(context);
    final Completer<ui.Image> completer = Completer<ui.Image>();
    final ImageStream stream = provider.resolve(config);
    ImageStreamListener listener;
    listener = ImageStreamListener(
          (ImageInfo image, bool sync) {
        completer.complete(image.image);
        stream.removeListener(listener);
      },
      onError: (dynamic exception, StackTrace stackTrace) {
        completer.complete();
        stream.removeListener(listener);
          FlutterError.reportError(FlutterErrorDetails(
            context: ErrorDescription('image failed to precache'),
            library: 'image resource service',
            exception: exception,
            stack: stackTrace,
            silent: true,
          ));
      },
    );
    stream.addListener(listener);

    return FutureBuilder(
        future: completer.future,
        builder: (BuildContext context, AsyncSnapshot<ui.Image> snapshot) {
          if (snapshot.hasData) {
            if (type == AsperctRaioImageType.FILE) {
              return filebBuilder(context, snapshot, file);
            } else if (type == AsperctRaioImageType.MEMORY) {
              return memoryBuilder(context, snapshot, bytes);
            } else {
              return builder(context, snapshot, url);
            }
          } else {
            return Container();
          }
        });
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,907评论 1 32
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 9,488评论 0 5
  • 下午一个聊的很好的同事说漏嘴,半年之内会跳槽。当时心里真的很有冲击感,离别总要习惯才行呀 自己都一年年考研要走,又...
    Bparanea阅读 1,950评论 0 0
  • 阅读1小时,总计657小时,第621日。 阅读《利维坦》至13% 在“利维坦”中,“主权”是使整体得到生命和活动的...
    龙套哥萨克海龙阅读 3,068评论 0 0
  • 内存分析 静态分析 Analyze 动态分析 Instruments 可以查看内存分配情况Allocations ...
    ChancePro阅读 1,045评论 0 0

友情链接更多精彩内容