Actionscript 3 加载未知数量的不带XML的外部图像
我需要知道如何在不使用XML的情况下从外部文件夹加载未知数量的图像 请帮帮我,Actionscript 3 加载未知数量的不带XML的外部图像,actionscript-3,Actionscript 3,我需要知道如何在不使用XML的情况下从外部文件夹加载未知数量的图像 请帮帮我, 谢谢你能把这个问题再详细一点吗?如果是用户操作(即用户需要上传一些照片),我会使用-您可以看到示例-否则,如果是从服务器端,我会使用PHP或Phyton脚本。假设您的应用程序是Air(桌面应用程序),此代码将非常有用: <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.c
谢谢你能把这个问题再详细一点吗?如果是用户操作(即用户需要上传一些照片),我会使用-您可以看到示例-否则,如果是从服务器端,我会使用PHP或Phyton脚本。假设您的应用程序是Air(桌面应用程序),此代码将非常有用:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
public function init():void{
var fs:FileStream = new FileStream();
var f:File = new File("c:/imgTest.jpg");
var b:ByteArray = new ByteArray();
fs.open(f, FileMode.READ);
fs.readBytes(b,0,fs.bytesAvailable);
idImg.source = b;
fs.close();
}
]]>
</mx:Script>
<mx:Image id="idImg" width="100%" height="100%"/>
</mx:WindowedApplication>
将图像放置在c:/imgTest.jpg中。请注意,此图像位于项目路径之外。
您可以使用其他选项加载图像,但这些图像必须可以通过URL访问,或者应该位于项目路径中。
以下链接将用于Flex Air和Web中的加载图像:
注意:我只尝试了JPG文件,我不知道这是否适用于其他类型。因此根据您的评论,我假设这是一个AIR应用程序,因此您可以通过
文件
类访问文件系统
首先,您需要获得一个指向文件夹的文件
对象,最简单的方法是对其进行硬编码。更复杂的方法是打开一个对话框,用户可以在其中选择所需的文件夹(使用)
让我们走简单的路线,定义一个到文件夹的固定路径,这里是users documents文件夹中名为“images”的文件夹:
File imageFolder = File.documentsDirectory.resolvePath("images");
一旦有了文件夹实例,我们就可以使用getDirectoryListing
方法列出该文件夹中的所有文件。下面是一个例子:
// create a vector that will contain all our images
var images:Vector.<File> = new Vector.<File>();
// first, check if that folder really exists
if(imageFolder.exists && imageFolder.isDirectory){
var files:Array = imageFolder.getDirectoryListing();
for each(var file:File in files){
// match the filename against a pattern, here only files
// that end in jpg, jpeg, png or gif will be accepted
if(file.name.match(/\.(jpe?g|png|gif)$/i)){
images.push(file);
}
}
}
// at this point, the images vector will contain all image files
// that were found in the folder, or nothing if no images were found
// or the folder didn't exist.
本地文件夹,远程文件夹?你试过什么?您看过flash.filesystem.File上的flash文档了吗?谢谢您的回复,我尝试从本地文件夹加载外部图像。@Nesrin是您的Web或桌面(air)项目?我在桌面应用程序中工作,我需要从包含图像的外部文件夹创建图库,但我不知道该图像的编号,因为编号可能会每次更改,因此我需要知道如何将其加载到应用程序中。您可以使用文件系统读取目录并获取该目录中的文件。如果外部文件夹处于联机状态,则此操作将不起作用。(文件夹在线吗?)我正在桌面应用程序中工作,我需要从包含图像的外部文件夹创建图库,但我不知道这些图像的编号,因为编号可能每次都会更改,所以我需要知道如何将它们加载到应用程序中
for each(var file:File in images){
// use a FileStream to read the file data into a ByteArray
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes);
stream.close();
// create a loader and load the image into it
var loader:Loader = new Loader();
// use the loadBytes method to read the data
loader.loadBytes(bytes);
// you can add the loader to the scene, so that it will be visible.
// These loaders will all be at 0, 0 coordinates, so maybe change
// the x and y coordinates to something more meaningful/useful.
this.addChild(loader);
}