| djyamato |
15.04.2011 07:24 |
ой, не получается вложить as файл
так напишу
Код AS3:
package{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.system.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.text.TextField;
public class picLoaderNoCrossdomain extends MovieClip {
private var ThisLoader;
private var picPath:String;
private var loader:Loader;
private var req:URLRequest;
private var par:*;
private var DrawedBitmap:Bitmap;
private var BD:BitmapData;
private var BoundsObject:Object;
private var Preloader:*;
private var scaleMatrix:Matrix;
private var BMP:Bitmap;
public function picLoaderNoCrossdomain (path:String,main:*,bObject,preload:*) {
ThisLoader=this;
par=main;
picPath=path;
BoundsObject=bObject;
Preloader=preload;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.handler_progress);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
//trace("loader load "+picPath);
var lc:LoaderContext = new LoaderContext();
lc.checkPolicyFile = true;
loader.load(new URLRequest(picPath),lc);
}
public function loadPicture(path:String):void{
picPath=path;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPicLoaded);
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, ThisLoader.handler_progress);
loader=null;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, ThisLoader.handler_progress);
var lc:LoaderContext = new LoaderContext();
lc.checkPolicyFile = true;
req=new URLRequest(picPath);
loader.load(req,lc);
}
public function destroyLoad():void{
loader.unload();
try{
loader.close();
}catch(someError:Error){
trace("no need to close");
}
ThisLoader.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPicLoaded);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPicLoaded);
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, ThisLoader.handler_progress);
}
private function onPicLoaded (e:Event):void{
ThisLoader.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPicLoaded);
ThisLoader.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handler_complete);
this.loader.loadBytes(this.loader.contentLoaderInfo.bytes, new LoaderContext(false, ApplicationDomain.currentDomain));
}
private function handler_complete(event:Event):void {
//trace("PIC loaded "+BoundsObject);
var bitmap:BitmapData = new BitmapData(loader.width, loader.height, true);
bitmap.draw(loader);
DrawedBitmap = new Bitmap(bitmap);
if(Preloader!=null){
if(Preloader is TextField){
Preloader.text="";
}
}
ThisLoader.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.handler_complete);
if(BoundsObject!=null){
resizePicture();
}else{
par.dispatchEvent(new CustomEvent("fotoOpened",DrawedBitmap,null,null));
}
}
private function handler_progress(event:ProgressEvent):void{
if(Preloader!=null){
if(Preloader is TextField){
Preloader.text=Math.round(event.bytesLoaded/event.bytesTotal*100)+"%";
}else{
}
}
}
private function errorHandler(event:IOErrorEvent):void{
//eventsReceiver.dispatchEvent(new CustomEvent("ioError",event.text,null,null));
trace("!!!! IOErrorHandler "+event.text);
if(Preloader!=null){
if(Preloader is TextField){
Preloader.text="";
}else{
}
}
}
private function resizePicture() {
var tmpBitmap:Bitmap=new Bitmap();
var CountedWidth:Number=DrawedBitmap.width;
var CountedHeight:Number=DrawedBitmap.height;
var ScaleRatio:Number=1;
var tmpMatrix:Matrix=new Matrix();// матрица ресайза по ширине
var ConcatMatrix=new Matrix(); // матрица ресайза по высоте (если понадобится)
if (DrawedBitmap.width>BoundsObject.w) {
ScaleRatio=DrawedBitmap.width/BoundsObject.w;
tmpMatrix.scale(1/ScaleRatio,1/ScaleRatio);
CountedWidth=Math.round(DrawedBitmap.width/ScaleRatio);
CountedHeight=Math.round(DrawedBitmap.height/ScaleRatio);
}
if (CountedHeight>BoundsObject.h) {
ScaleRatio=CountedHeight/BoundsObject.h;
CountedWidth=Math.round(CountedWidth/ScaleRatio);
CountedHeight=Math.round(CountedHeight/ScaleRatio);
ConcatMatrix.scale(1/ScaleRatio,1/ScaleRatio);
}
tmpMatrix.concat(ConcatMatrix);
var tmpBitmapData:BitmapData=new BitmapData(Math.round(CountedWidth),Math.round(CountedHeight),true,0xFF0000);
//var tmpBitmapData:BitmapData=new BitmapData(604,300,false,0xFF0000);
tmpBitmapData.draw(DrawedBitmap,tmpMatrix);
BMP=new Bitmap(tmpBitmapData);
trace("BMP w="+BMP.width+" h="+BMP.height);
BMP.smoothing=true;
/////////////////////////////////////////////////////////
par.dispatchEvent(new CustomEvent("fotoOpened",BMP,null,null));
}
}
}
Это я чуть переделал класс, для загрузки XML-я, кажется, выложенный на форуме (автора не помню, к сожалению) , картинки грузит откуда угодно и ресайзит
Чуши в нем много (моей), простите.
|