Android 将大量图像加载到图像视图中
我正在将大量图像加载到钛合金的图像视图中。问题是,在安卓系统中,我遇到了内存问题。这是我将图像放入图像视图的功能。在这个函数中,我正在调整图像的大小Android 将大量图像加载到图像视图中,android,memory-management,titanium,appcelerator,Android,Memory Management,Titanium,Appcelerator,我正在将大量图像加载到钛合金的图像视图中。问题是,在安卓系统中,我遇到了内存问题。这是我将图像放入图像视图的功能。在这个函数中,我正在调整图像的大小 exports.SetImg = function(img, hightFactor, widthFactor, viewObj, deviceWidth, deviceHeight) { if (img != null && img != "") { var IMGhight = 0,
exports.SetImg = function(img, hightFactor, widthFactor, viewObj, deviceWidth, deviceHeight) {
if (img != null && img != "") {
var IMGhight = 0,
IMGwidth = 0,
height = 0,
width = 0;
var imgTemp = Ti.UI.createImageView({
image : img
});
if (imgTemp != null) {
if (imgTemp.toBlob().height != null && imgTemp.toBlob().width != null) {
IMGhight = imgTemp.toBlob().height;
IMGwidth = imgTemp.toBlob().width;
height = alturaImg;
width = larguraImg;
if (height > deviceHeight * hightFactor) {
if (height > deviceHeight * hightFactor) {
height = deviceHeight * hightFactor;
width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
}
if (width < deviceWidth * widthFactor) {
width = deviceWidth * widthFactor;
height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
}
} else if (width > deviceWidth * widthFactor) {
if (width > deviceWidth * widthFactor) {
width = deviceWidth * widthFactor;
height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
}
if (height < deviceHeight * hightFactor) {
height = deviceHeight * hightFactor;
width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
}
} else if (height < deviceHeight * hightFactor) {
if (height < deviceHeight * hightFactor) {
height = deviceHeight * hightFactor;
width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
}
if (width < deviceWidth * widthFactor) {
height = deviceWidth * widthFactor;
height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
}
} else if (width < deviceWidth * widthFactor) {
if (width < deviceWidth * widthFactor) {
width = deviceWidth * widthFactor;
height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
}
if (hight < deviceHeight * hightFactor) {
hight = deviceHeight * hightFactor;
width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
}
}
var imagem = Ti.UI.createImageView({
width : width,
height : hight,
image : img
});
viewObj.add(imagem);
imagem = null;
imgTemp = null;
IMhight = null;
IMGwidth = null;
hight = null;
width = null;
img = null;
hightFactor = null;
widthFactor = null;
viewObj = null;
deviceWidth = null;
deviceHeight = null;
}
}
}
};
exports.SetImg=函数(img、hightFactor、widthFactor、viewObj、deviceWidth、deviceHeight){
如果(img!=null&&img!=“”){
var IMGhight=0,
IMGwidth=0,
高度=0,
宽度=0;
var imgTemp=Ti.UI.createImageView({
图片:img
});
如果(imgTemp!=null){
if(imgTemp.toBlob().height!=null&&imgTemp.toBlob().width!=null){
IMGhight=imgTemp.toBlob().高度;
IMGwidth=imgTemp.toBlob().width;
高度=alturaImg;
宽度=最大宽度;
if(高度>设备高度*高度因子){
if(高度>设备高度*高度因子){
高度=设备高度*高度系数;
宽度=((设备高度*高系数)/IMGhight)*IMGwidth);
}
if(宽度<设备宽度*宽度系数){
宽度=设备宽度*宽度系数;
高度=((设备宽度*宽度系数)/IMGwidth)*IMGhight);
}
}否则如果(宽度>设备宽度*宽度系数){
如果(宽度>设备宽度*宽度系数){
宽度=设备宽度*宽度系数;
高度=((设备宽度*宽度系数)/IMGwidth)*IMGhight);
}
if(高度<设备高度*高度系数){
高度=设备高度*高度系数;
宽度=((设备高度*高系数)/IMGhight)*IMGwidth);
}
}否则如果(高度<设备高度*高度系数){
if(高度<设备高度*高度系数){
高度=设备高度*高度系数;
宽度=((设备高度*高系数)/IMGhight)*IMGwidth);
}
if(宽度<设备宽度*宽度系数){
高度=设备宽度*宽度系数;
高度=((设备宽度*宽度系数)/IMGwidth)*IMGhight);
}
}否则如果(宽度<设备宽度*宽度系数){
if(宽度<设备宽度*宽度系数){
宽度=设备宽度*宽度系数;
高度=((设备宽度*宽度系数)/IMGwidth)*IMGhight);
}
if(高度<设备高度*高度系数){
hight=设备高度*高系数;
宽度=((设备高度*高系数)/IMGhight)*IMGwidth);
}
}
var imagem=Ti.UI.createImageView({
宽度:宽度,
高度:高度,
图片:img
});
添加视图对象(imagem);
imagem=null;
imgTemp=null;
IMhight=null;
IMGwidth=null;
hight=null;
宽度=零;
img=null;
hightFactor=null;
宽度因子=空;
viewObj=null;
deviceWidth=null;
deviceHeight=null;
}
}
}
};
我已经为GC将所有变量声明为null,但仍然存在内存问题。现在有人知道如何解决这个问题了吗
问题是,我需要将相同的图像重新用于其他内容,因此我需要将它们保存起来。您应该真正调整图像的大小,并将其保存到缩略图文件中。而不仅仅是调整imageview的大小并在其中显示整个图像。那会帮你节省一些记忆 另外,您正在创建多个blob
imgTemp.toBlob()
。运行此命令一次并重用blob变量。所有其他计算都是一样的。这并不多,但当您只需计算一次并重用该值时,可以节省一些时间/空间
但是你需要调整图像的大小,也许可以使用一个定制的android模块,如果你需要的话,它允许批量调整整个文件夹或文件数组的大小。将
imgTemp.toBlob()
提取到一个公共变量将对你的内存消耗产生巨大的积极影响。看看封面下发生了什么,实际上每次调用toBlob()
,都会多次将图像加载到内存中!我在你的代码中看到了4个用法,加上实际的图像视图用法,乘以30个图像,你已经得到了数百个位图副本
请参阅此处的源代码以获取更多证据--
在TiUIImageView.java中:
在TiBlob.java中:
若您从服务器接收图像,那个么最好将调整大小的图像放在服务器上。服务器上图像的分辨率可以是您将在应用程序中创建ImageView的最大宽度*高度 现在,若您要在更大的ImageView中的其他屏幕上使用这些图像(比如在全屏上显示图像),那个么对于这两种情况,您可以在服务器上拥有不同的文件夹。e、 g
- 对于较小的ImageView,请在服务器上创建一个缩略图文件夹
- 对于较大的ImageView,请创建另一个名为图片的文件夹
- 现在,假设您在服务器上的实际图像是my_image.jpg,大小宽度=1024,高度=800,然后放置300*234的缩略图,使纵横比与此路径相同缩略图/my_image.jpg和此路径上的实际图像图片/my_image.jpg