【问题标题】:Downloading Transparent PNG's on Android using Appcelerator turns the background black使用 Appcelerator 在 Android 上下载透明 PNG 会使背景变黑
【发布时间】:2015-04-13 12:45:35
【问题描述】:

谁能解释为什么使用Image Factory module在Android上下载和存储图像,它会忽略PNG图形的透明度并给它们一个黑色背景?

它在 iOS 上运行良好,一切都“原样”。

是否需要在下载脚本中添加任何内容以保持透明度?

救命!

这是我的下载脚本,我正在使用 Titanium 3.5.1 GA 构建:

function getMarker(url, filename) {

// this will enable us to have multiple file sizes per device
var filename2 = filename.replace(".png", "@2x.png");
var filename3 = filename.replace(".png", "@3x.png");

var mapMarker = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map_marker_icons', filename);
var mapMarker2 = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map_marker_icons', filename2);
var mapMarker3 = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map_marker_icons', filename3);

// now we need to download the map marker and save it into our device 

var getMarker = Titanium.Network.createHTTPClient({
    timeout: 30000
});

getMarker.onload = function() {

    // if the file loads, then write to the filesystem
    if (getMarker.status == 200) {
        // resize the images into non-retina, retina and retina HD and only download and resize what is actyally required

            var getOriginal = ImageFactory.imageWithAlpha(this.responseData, {});

            var resized2 = ImageFactory.imageAsResized(getOriginal, {
                width: 50,
                height: 50
            });
            mapMarker.write(resized2);
            Ti.API.info(filename + " Image resized");


        //I ALWAYS NULL ANY PROXIES CREATED SO THAT IT CAN BE RELEASED
        mapMarker = null;
    } else {
        Ti.API.info("Image not loaded");
    }

    // load the tours in next
    loadNav();



};

getMarker.onerror = function(e) {
    Ti.API.info('XHR Error ' + e.error);
    //alert('markers data error');
};

getMarker.ondatastream = function(e) {
    //Ti.API.info('Download progress: ' + e.progress);
};

// open the client
getMarker.open('GET', url);

// change the loading message
MainActInd.message = 'Downloading Markers';
// show the indicator
MainActInd.show();

// send the data
getMarker.send();    }

任何帮助将不胜感激!

西蒙

【问题讨论】:

  • 如果你要保存getOriginal,它也有变化吗?
  • 是的,我也试过了,不高兴,忘了我把它放在那里了,但是它仍然以黑色背景下载
  • 那么不要使用 ImageFactory 来下载文件。我不知道钛,但应该有一些东西可以将 responseData 直接定向到我希望你的文件/输出流。

标签: android titanium png appcelerator appcelerator-mobile


【解决方案1】:

请尝试以下代码,我在 Android 和 iOS 上使用 png Url 进行了尝试,首先我使用 HTTP 客户端请求获取照片,然后将其保存为扩展名为 png 的文件,然后使用 @987654321 读取它@。

index.js:

$.win.open();

savePng("https://cdn1.iconfinder.com/data/icons/social-media-set/29/Soundcloud-128.png");
function savePng(pngUrl) {
    var client = Titanium.Network.createHTTPClient({
        onload : function(e) {
            var image_file = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "test.png");
            image_file.write(this.responseData);
            $.img.image = image_file.read();
        },
        onerror : function(e) {
            alert(e.error);
        },
        timeout : 10000
    });
    client.open("GET", pngUrl);
    client.send();
}

index.xml:

<Alloy>
        <Window id="win" backgroundColor="gray">
        <ImageView id="img" />
    </Window>
</Alloy>

【讨论】:

  • 这些特定的图像用于地图标记,所以我不能使用图像视图来显示它们。我会看看在原始下载中是否不使用图像工厂,比如你的 sn-p,是否可以完成这项工作,然后我可以稍后进行调整大小。
  • 好的,像你的建议一样保存它,没有黑色背景!现在只需要调整图像大小,希望这很容易。
  • 您不必使用图像视图,我只是证明它们没有黑色背景..
  • 试试这个代码来调整resizedImage = image.imageAsResized(200, 300);的大小,在图片this.responseData上试试,如果它不起作用,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2015-04-20
  • 2013-09-26
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 2012-08-30
  • 2014-11-12
  • 2011-12-27
相关资源
最近更新 更多