Flutter项目中无用图片的检测及清理

一、背景

随着项目规模或者迭代周期的增加,项目中会保留部分已经不在使用的图片,这些资源的存在,会增加包体积。
在优化app大小时,常采用的手段之一就是对不在使用的图片进行清理。

二、原理

在flutter中使用asset中的图片的时候,都是根据图片路径加载图片,如:Image.asset(“assets/account/watsapp.png”),
根据图片的这样使用方式,我们就可以使用图片的名字或者图片的相对路径(assets/account/watsapp.png),在dart文件中搜索,如果在dart文件中我们找到该匹配的字符串,我们就认为该图片已经存在。
也存在误判的情况,我们在异常说明这部分中解释说明,也欢迎大家补充

三、实现方法

使用Python,获取项目中所有添加的图片绝对路径,并用存储到List中,然后读取dart文件,看在dart文件中是否存在与图片名称(watsapp.png)或者图片相对路径(assets/account/watsapp.png)相同的字符串,如果找到匹配的字符串,就认为图片在项目中使用,然后将图片地址从集合中移除,分析完所有dart文件,最后还在集合中的图片就可以认为是项目中已经不在使用的图片。

以下是使用Python3实现的完整代码:

#!/user/local/bin/python3
#将本文件放在Flutter项目的根目录

from genericpath import isdir
import imghdr
from operator import delitem
import os
from re import search
import re
import string
from sys import path
import sys
from tokenize import String
from typing import List

print("---Analyze unused Assets----")
#项目目录
projectAbsRootPath = sys.path[0]
#图片所在的资源目录路径
assetPath="/assets"
#项目中dart代码所在目录
libPath = projectAbsRootPath+ "/lib"
assetAbPath = projectAbsRootPath+assetPath

print("projectRootPath:" + projectAbsRootPath +  "   assets:" +assetAbPath + "     lib:" + libPath
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。 对于获取Android照片地址,可以使用flutter的image_picker插件来实现。首先在pubspec.yaml文件添加依赖: ``` dependencies: image_picker: ^0.6.7+22 ``` 然后在需要获取照片的位置调用以下代码即可: ```dart import 'package:image_picker/image_picker.dart'; final picker = ImagePicker(); final pickedFile = await picker.getImage(source: ImageSource.gallery); if (pickedFile != null) { final String path = pickedFile.path; // do something with the image path } ``` 对于Flutter项目安卓配置启动图片,可以在Android的项目目录下的`android/app/src/main/res`文件夹放置启动图片。具体命名规则和尺寸可以参考Flutter官方文档:https://flutter.dev/docs/development/ui/advanced/splash-screen。在Android的`AndroidManifest.xml`文件添加以下代码即可: ```xml <application android:name="io.flutter.app.FlutterApplication" android:icon="@mipmap/ic_launcher" android:label="your_app_name" android:theme="@style/LaunchTheme"> <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> ``` 其`@drawable/launch_background`指的是启动图片的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值