A闪的 BLOG 技术与人文
很多朋友在使用flash技术开发ios应用的时候都会遇到加载外部图片资源的问题。举个简单的例子,假如我们要实现换皮肤的功能就会使用到加载外部图片资源。看到不少朋友在网上提问,说AIR在IOS中运行的时候就无法进行资源加载,再次特意写了一个简单的DEMO为大家简单介绍一下。
为了确保我们能够简单而且直观的看到效果,我的demo中仅仅是使用了一个loader对象来进行显示。那么如何证明我们确实可以正常无误的加载资源呢?这里我采用了两种方法,首先我在本地资源中放入了一张jpg格式的图片。其次为了进行测试,我在我的macbook中架设了一个Apache运行环境,并通过局域网Ip地址访问服务器内资源。
先简单来看一下代码:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
public class loadtest extends Sprite
{
private var loader:Loader;
public function loadtest()
{
super();
// 支持 autoOrient
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
//
this.loader = new Loader();
this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,ok);
var a:Sprite = new Sprite();
a.graphics.beginFill(0x00ff00);
a.graphics.drawRect(0,0,100,100);
a.graphics.endFill();
a.addEventListener(MouseEvent.CLICK,dian);
this.addChild( a );
var t:Sprite = new Sprite();
t.graphics.beginFill(0x000000);
t.graphics.drawRect(0,0,100,100);
t.graphics.endFill();
t.addEventListener(MouseEvent.CLICK,tt);
t.y = 150;
this.addChild(t);
}
private function tt(evt:MouseEvent):void
{
this.loader.load( new URLRequest("image.jpg") );
}
private function dian(evt:MouseEvent):void
{
this.loader.load( new URLRequest("http://192.168.0.102/image.jpg") );
}
private function ok(evt:Event):void
{
this.loader.x = this.loader.y = 200;
this.addChild( this.loader );
}
}
}
如果利用原来的flash技术我们可以非常确信的加载到我们想要的资源,在IOS上面,这段代码同样有效。下面是测试后的结果截图:
在IOS中运行的代码和PC中是完全相同的。当我们使用网络加载的时候,我们的image会被加载到内存当中,但是当我们使用本地图片的时候,我们可以默认理解为图片就存放到ios系统的硬盘中,如果程序部队图片进行加载,那么我们也就不会因为图片资源占用更多的内存。