Copy link to clipboard
Copied
Hi. I'm using Flash CS6 to create an AIR app that loads an image from the user's computer and then does a pixel count using its bitmap data.
MyImg is a class that represents the loaded image.
package com.utils{
import flash.display.Loader;
public class MyImg extends Loader {
public function MyImg ():void {
}
}
}
In my main class I handle the file selection and loading.
private var _myImage:MyImg = new MyImg();
private var _file:FileReference;
private var _fileTypes:FileFilter = new FileFilter("Imagens: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg; *.jpg; *.gif; *.png");
When a "load image" button on the stage is clicked, it calls loadMyImg().
public function loadMyImg($e:MouseEvent):void {
chooseFile();
}
Then the user chooses the file he wants to load.
private function chooseFile():void {
_file = new FileReference();
_file.addEventListener(Event.SELECT, fileSelected);
_file.browse([_fileTypes]);
}
Once the file is selected, it's loaded as a byte array.
private function fileSelected($e:Event):void {
_file.removeEventListener(Event.SELECT, fileSelected);
_file.addEventListener(Event.COMPLETE, fileLoaded);
_file.load();
}
When it finishes loading it loads the byte array into _myImage.
private function fileLoaded($e:Event):void {
_file.removeEventListener(Event.COMPLETE, fileLoaded);
_myImage.contentLoaderInfo.addEventListener(Event.COMPLETE, dataReady);
_myImage.loadBytes(_file.data);
}
Once that is done I try to use the _myImage's BitmapData, but can't...
private function dataReady($e:Event):void {
$e.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, dataReady);
trace ($e.target + $e.target.content + $e.target.content.name + $e.target.content.bitmapData); //returns [object LoaderInfo] [object Bitmap] instance 155 [object BitmapData]
trace (_myImage + _myImage.content + _myImage.content.name + _myImage.contentLoaderInfo + _myImage.contentLoaderInfo.content.name); //returns [object MyImg] [object Bitmap] instance 155 [object LoaderInfo] instance 155
trace (_myImage.content.bitmapData) //compiler error: 1119 Access of possibly undefined property bitmapData through a reference with static type flash.flashDisplay:DisplayObject.
trace (_myImage.contentLoaderInfo.content.bitmapData) //same error
}
I don't understand what I'm doing wrong. The instances have the same name, so they must be the same. They both trace as bitmaps, yet when I don't access it from the event I don't get the bitmapData. Can anyone tell me what I'm doing wrong? I need to use the bitmapData from inside the MyImg class. I guess I could create some BitmapData variable in the class and give it the value of $e.target.content.bitmapData, but I'd still like to know why it can't be simpler.
Thanks!
cast _myImage.content as bitmap:
Bitmap(_myImage.content)
Copy link to clipboard
Copied
cast _myImage.content as bitmap:
Bitmap(_myImage.content)
Copy link to clipboard
Copied
Thanks kglad, that worked.
I still don't know why I have to cast it as a bitmap when I trace (_myImage.content) and it returns [object Bitmap], though.
Copy link to clipboard
Copied
the compiler gets easily confused. there are many situations where you need to explicitly cast an object to something that seems like it should be obvious.
Copy link to clipboard
Copied
Ahhh, I see. It complicates things but at least I'll know what to watch out for in future problems. Again, thanks for the help!
Copy link to clipboard
Copied
you're welcome.