【问题标题】:how to get a function to activate which is ment to be used with如何获得要使用的功能来激活
【发布时间】:2014-04-03 00:06:03
【问题描述】:

我有这段代码,如果我从外部 XML 文件中获取一个图像链接,用它加载它...

<mx:Label
id="textboxLink" text=""/>

private function loadRemoteImg(url:String):void { 

textboxLink.text
.....
loader, completeHandler etc.

Save Image(), with ByteArray, JPEGEcoder and then to the location - filestream etc.

这一切正常,但只有通过 MouseEvent 才能实现(由于据说是 Flash Player 10 及更高版本),因此只需单击一个按钮!

如前所述,一切正常,但我真的需要在启动时激活它,就像在 creationComplete 中一样!

任何帮助或任何想法都会被应用!关于aktell

【问题讨论】:

  • 你是如何加载你的图片的?如果您只是从 XML 中获取 url,那么普通的 Loader 应该可以工作。鼠标点击限制通常仅用于打开网址(navigateToURL
  • 一切正常,它也是一个加载器,问题是它只能通过 Btn 点击激活!使用这个。 click="loadRemoteImage(textboxLink.text)".
  • textboxLink.text 只是一个带有您的网址的标签。如何加载它完全取决于您 - 您刚刚选择使用 click 操作加载它;您可以轻松地使用TimersetInterval,甚至是键盘事件;当您知道该标签已被填充时,您只需要调用它
  • 不,完全没有,因为我已经一次又一次地尝试了它,它只能通过点击 Btn 来工作。我发现的唯一一件事是提到 Flash Player 10 以后它发生了变化如果不用于 btn 点击!
  • 刚刚按照建议用定时器试了一下!根本没有发生任何错误消息,但它也没有激活!问候阿克特尔

标签: actionscript-3 apache-flex air flex3 flexbuilder


【解决方案1】:

啊,抱歉,我以为你只是想加载图像;我没看到你也想保存它。

对于以下两种情况,您都需要将图像加载为二进制:

var urlLoader:URLLoader = new URLLoader(new URLRequest( myURL ));
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
...

这是因为如果我们正常加载它(使用Loader),那么我们将得到一个Bitmap 对象,即Flash 的图像表示,而不是jpg/png 数据本身。使用此方法加载它会在加载时为您提供ByteArray

如果您使用的是 AIR,您应该可以使用 FileStream 类:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html

类似:

// NOTE: you can use any of the other File constants to get your path (e.g.
// documentsDirectory, desktopDirectory...
// myImageFileName is the name of your image, e.g. "myPic.jpg"
var file:File       = File.applicationStorageDirectory.resolvePath( myImageFileName );
var fs:FileStream   = new FileStream;
try
{
    fs.open( file, FileMode.WRITE );
    fs.writeBytes( imageBinaryData ); // imageBinaryData is a ByteArray
    fs.close();
}
catch ( e:Error )
{
    trace( "Can't save image: " + e.errorID + ": " + e.name + ": " + e.message );
}

如果您使用的是 Flash,那么无需用户交互即可保存内容的唯一方法是通过SharedObject。这意味着您的数据将无法在应用程序之外使用(它将是一个.sol 文件),但根据您的操作方式,这可能不是问题。

// get our shared object - if you're saving a lot of images, then you might need another shared
// object, whose name you know, that stores the names of the other images
var so:SharedObject = null;
try { so = SharedObject.getLocal( this.m_name ); }
catch ( e:Error )
{
    trace( "Couldn't get shared object: " + e.errorID + ": " + e.name + ": " + e.message );
    return;
}

// NOTE: it's possible you may need a registerClassAlias on the ByteArray before this
so.data["image"] = imageBinaryData;

// save the lso
try { so.flush( minDiskSpace ); }
catch ( e:Error )
{
    trace( "Couldn't save the lso: " + e.errorID + ": " + e.name + ": " + e.message );
}

以后要实际使用您的图像,请加载您的文件(以二进制模式)/获取您的SharedObject,并将保存的二进制文件转换为图像:

var l:Loader = new Loader;
l.contentLoaderInfo.addEventListener( Event.COMPLETE, this._onConvertComplete ); // you could probably also listen for the IO_ERROR event
try
{
    // NOTE: you can pass a LoaderContext to the loadBytes methods
    l.loadBytes( imageBinaryData );
}
catch( e:Error )
{
    trace( "Couldn't convert image: " + e.errorID + ": " + e.name + ": " + e.message );
}

...

// called when our loader has finished converting our image
private function _onConvertComplete( e:Event ):void
{
    // remove the event listeners
    var loaderInfo:LoaderInfo = ( e.target as LoaderInfo );
    loaderInfo.removeEventListener( Event.COMPLETE, this._onConvertComplete );

    // get our image
    var bitmap:Bitmap = loaderInfo.content;
    this.addChild( bitmap );
}

如果您不能使用这些方法中的任何一种,那么您将不得不进行某种用户交互(例如鼠标单击)。 (出于好奇,您是否尝试过在相关对象上分派MouseEvent.CLICK,看看它是否可行?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2020-02-13
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多