开发者

Convert and String(path of image) to a Bitmap obj

开发者 https://www.devze.com 2023-02-03 02:19 出处:网络
The solution should be straight forward but I cannot find it, my problem it\'s that I\'m reading a xml, and one of the properties inside the xml it\'s a Bitmap path(string), when I`m reading this开发者

The solution should be straight forward but I cannot find it, my problem it's that I'm reading a xml, and one of the properties inside the xml it's a Bitmap path(string), when I`m reading this开发者_开发技巧 xml I would like to convert this string to a Bitmap Obj so I can use through my MXMLs.

Some suggestion?!


I would load the bitmapData like heartcode wrote in their answer.

But to retreive the BitmapData in the loaded function I would do this

var ldr:Loader = event.currentTarget as Loader;    
var bitmap:Bitmap = Bitmap(ldr.content);
var bd:BitmapData = new Bitmap(bitmap.bitmapData.clone(), "auto", true);
ldr.removeEventListener(Event.COMPLETE, addBitmap);
ldr = null; //Allow flex to gc the Loaded as you now have two copies of the bytes in memory

Using the draw method, I think the loader will need to be on the stage so it can be rendered. This will avoid that. You can the assign bd as the source of the BitmapImage


if you need the bitmapdata or the bitmap, try this:

import flash.display.*;
import flash.events.*;
import flash.net.*;

var bitmapPath:String = "imageFilePath";
var myBitmap:Bitmap;
var bitmapLoader:Loader;

bitmapLoader = new Loader();
bitmapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addBitmap);
bitmapLoader.load(new URLRequest(bitmapPath));

function addBitmap(event:Event):void
{
    var bd:BitmapData = new BitmapData(event.currentTarget.content.width, event.currentTarget.content.height);
    bd.draw(event.currentTarget.content);

    myBitmap = new Bitmap(bd);
    addChild(myBitmap);
};


If you're using MXML, you should just be able to pass the string directly to the "source" attribute of the tag. Flex abstracts away all of the loading stuff.


I posted some code that may help for a similar question that was asked a little while ago. It will take your XML contents and preload the images into an ArrayCollection to use later.

Embeding images in an Flex application

0

精彩评论

暂无评论...
验证码 换一张
取 消