gpt4 book ai didi

android - 将大量图像加载到钛 ImageView 中

转载 作者:行者123 更新时间:2023-11-29 17:16:32 24 4
gpt4 key购买 nike

我正在将大量图像加载到钛合金 ImageView 中。问题是在android中我遇到了内存问题。这是我将图像放入 ImageView 的功能。在这个函数中,我正在调整图像的大小。

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 将所有变量声明为 null,但我仍然遇到内存问题。现在有人如何解决这个问题吗?

This is the layout that I need do make.

问题是我需要为其他东西重用相同的图像,所以我需要它们求助。

最佳答案

imgTemp.toBlob() 提取到一个公共(public)变量将对您的内存消耗产生巨大的积极影响。看看幕后发生的事情,您实际上每次调用 toBlob() 时都会多次将图像加载到内存中!我在您的代码中看到 4 次使用加上实际 ImageView 使用,乘以 30 张图像,您已经得到了数百份位图副本。

查看源代码以获得更多证明--

在 TiUIImageView.java 中: https://github.com/appcelerator/titanium_mobile/blob/e6a5f6c086a019dbdf810e225bb13c5c8d9115ac/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIImageView.java#L945

在 TiBlob.java 中: https://github.com/appcelerator/titanium_mobile/blob/415bd6c66dcc55b1a59a59574f3babd3c3a84ede/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java

关于android - 将大量图像加载到钛 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38592134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com