将大量图像加载到钛图像视图中
load lot of images into titanium image view
我正在将大量图像加载到钛合金图像视图中。问题是在 android 中我遇到了内存问题。这是我将图像放入图像视图的功能。在这个函数中,我正在调整图像的大小。
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;
}
}
}
};
我已经为 GC 声明了所有变量为空,但我仍然遇到内存问题。现在有人如何解决这个问题吗?
问题是我需要将相同的图像重复用于其他用途,所以我需要它们来求助。
您确实应该调整图像大小并将其保存为缩略图文件。而不仅仅是调整图像视图的大小并在其中显示整个图像。那会为你节省一些记忆。
您还创建了多个 blob imgTemp.toBlob()
。 运行 此命令一次并重复使用 blob 变量。所有其他计算都相同。它并不多,但是当您只计算一次并重新使用该值时,可以为您节省一些 time/space。
但是您需要调整图像大小,也许可以使用自定义 android 模块,如果您需要的话,该模块允许对整个文件夹或文件数组进行批量调整。
将 imgTemp.toBlob()
提取到一个公共变量将对您的内存消耗产生巨大的积极影响。看看幕后发生的事情,您实际上每次调用 toBlob()
时都会多次将图像加载到内存中!我在您的代码中看到 4 次使用加上实际图像视图使用,乘以 30 张图像,您有数百份位图被扔掉了。
在此处查看源代码以获得更多证明 --
在TiUIImageView.java中:
https://github.com/appcelerator/titanium_mobile/blob/e6a5f6c086a019dbdf810e225bb13c5c8d9115ac/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIImageView.java#L945
如果您从服务器接收图像,那么最好将调整后的图像放在服务器上。服务器上的图像分辨率可以是您在应用程序中创建 ImageView 的最大宽度*高度。
现在,如果您打算在更大的 ImageView 中的其他屏幕上使用这些图像(例如全屏显示图像),那么您可以在服务器上为这两种情况使用不同的文件夹。例如
- 对于较小的 ImageView,在服务器
上创建一个 thumbnail 文件夹
- 对于更大的 ImageView,创建另一个名为 pictures.
的文件夹
- 现在,假设您在服务器上的实际图像是 my_image.jpg 大小 width = 1024,height = 800,然后放置 300*234 的缩略图保持纵横比比例与此路径 thumbnail/my_image.jpg 和此路径的实际图像 pictures/my_image.jpg
同样,如果你真的想根据你的要求调整图像的大小,那么Titanium Blob API like imageAsThumbnail
中有非常简洁的方法
此外,如果您想一次加载 30 张 1024*800 的图像,那么您将不得不研究延迟加载方法,因为如果您创建 30 张 1024*800 的 ImageView一次。
最后,如果你能提示我们你想设计什么样的 UI ,这样我们就可以建议你更好的方法来做到这一点,而不仅仅是调整图像大小和执行许多计算他们。
我正在将大量图像加载到钛合金图像视图中。问题是在 android 中我遇到了内存问题。这是我将图像放入图像视图的功能。在这个函数中,我正在调整图像的大小。
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;
}
}
}
};
我已经为 GC 声明了所有变量为空,但我仍然遇到内存问题。现在有人如何解决这个问题吗?
问题是我需要将相同的图像重复用于其他用途,所以我需要它们来求助。
您确实应该调整图像大小并将其保存为缩略图文件。而不仅仅是调整图像视图的大小并在其中显示整个图像。那会为你节省一些记忆。
您还创建了多个 blob imgTemp.toBlob()
。 运行 此命令一次并重复使用 blob 变量。所有其他计算都相同。它并不多,但是当您只计算一次并重新使用该值时,可以为您节省一些 time/space。
但是您需要调整图像大小,也许可以使用自定义 android 模块,如果您需要的话,该模块允许对整个文件夹或文件数组进行批量调整。
将 imgTemp.toBlob()
提取到一个公共变量将对您的内存消耗产生巨大的积极影响。看看幕后发生的事情,您实际上每次调用 toBlob()
时都会多次将图像加载到内存中!我在您的代码中看到 4 次使用加上实际图像视图使用,乘以 30 张图像,您有数百份位图被扔掉了。
在此处查看源代码以获得更多证明 --
在TiUIImageView.java中: https://github.com/appcelerator/titanium_mobile/blob/e6a5f6c086a019dbdf810e225bb13c5c8d9115ac/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIImageView.java#L945
如果您从服务器接收图像,那么最好将调整后的图像放在服务器上。服务器上的图像分辨率可以是您在应用程序中创建 ImageView 的最大宽度*高度。
现在,如果您打算在更大的 ImageView 中的其他屏幕上使用这些图像(例如全屏显示图像),那么您可以在服务器上为这两种情况使用不同的文件夹。例如
- 对于较小的 ImageView,在服务器 上创建一个 thumbnail 文件夹
- 对于更大的 ImageView,创建另一个名为 pictures. 的文件夹
- 现在,假设您在服务器上的实际图像是 my_image.jpg 大小 width = 1024,height = 800,然后放置 300*234 的缩略图保持纵横比比例与此路径 thumbnail/my_image.jpg 和此路径的实际图像 pictures/my_image.jpg
同样,如果你真的想根据你的要求调整图像的大小,那么Titanium Blob API like imageAsThumbnail
中有非常简洁的方法此外,如果您想一次加载 30 张 1024*800 的图像,那么您将不得不研究延迟加载方法,因为如果您创建 30 张 1024*800 的 ImageView一次。
最后,如果你能提示我们你想设计什么样的 UI ,这样我们就可以建议你更好的方法来做到这一点,而不仅仅是调整图像大小和执行许多计算他们。