【问题标题】:Flex widget load images from JSON, not all images displayedFlex 小部件从 JSON 加载图像,而不是显示所有图像
【发布时间】:2014-12-30 03:36:48
【问题描述】:

我正在尝试在从 JSON 获取的 flex 小部件中显示图像。除了在小部件中显示图像之外的所有工作。

我在小部件的设计模式中有 12 个预定义的图像,当从 JSON 获取数据时,将在运行时分配源。图像 1 到图像 4 用于劣质图像,图像 5 到图像 8 用于普通图像,图像 9 到图像 12 用于豪华图像。 (其实我是想在运行时创建图片数组,但是没找到合适的例子。)

我使用同一组 JSON 数据进行测试。奇怪的是每次widget中显示的图片都不一样,比如有时候会显示所有图片,有时候会显示image2、image4、image7,有时候会显示image1、image2、image5。

我使用的源码:

private var value0:String;//id
private var value1:String;// type
private var value2:String;// image url

var request:URLRequest=new URLRequest();
request.url=getUrl;
request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
request.method=URLRequestMethod.GET;
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE, receive);
loader.load(request);

protected function receive(event:Event):void
{
var myResults:Array= com.adobe.serialization.json.JSON.decode(event.target.data);

for (var i:int =0;i<myResults.length; i++){
  value0 = myResults[i].goods_id;
  value1 = myResults[i].goods_type;
  value2 = myResults[i].image_url;

  if (myResults[i].drawing_type == "INFERIOR"){
    if (!image1.source){
        image1.source = value2;
    }else if (!image2.source){
        image2.source = value2;
    }else if (!image3.source){
        image3.source = value2;
    }else if (!image4.source){
        image4.source = value2;
    }
  }

  if (myResults[i].drawing_type == "NORMAL"){
    if (!image5.source){
        image5.source = value2;
    }else if (!image6.source){
        image6.source = value2;
    }else if (!image7.source){
        image7.source = value2;
    }else if (!image8.source){
        image8.source = value2;
    }
  }

  if (myResults[i].drawing_type == "LUXURY"){
    if (!image9.source){
        image9.source = value2;
    }else if (!image10.source){
        image10.source = value2;
    }else if (!image11.source){
        image11.source = value2;
    }else if (!image12.source){
        image12.source = value2;
    }
  }
}

对此有任何想法吗?请指教,谢谢。

【问题讨论】:

    标签: json image actionscript-3 apache-flex widget


    【解决方案1】:

    分配 .source 属性有点棘手。您可以使用 flex Loader 在运行时加载图像并在运行时分配它。这样做可以让您找出在运行时和错误处理期间发生了哪些异常。下面的代码(见加载器_)

    http://ntt.cc/2008/03/09/tips-three-ways-to-load-an-image-file-in-flex.html

    【讨论】:

    • 非常感谢Zens,我尝试了第二种和第三种方式,但结果是一样的。
    • @user30643 您可能需要检查图像是否位于您指定的网址中。复制网址并在网络浏览器中查看,看看图像是否已涂漆
    • 谢谢宙斯,是的,我已经检查了找到的网址和图片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多