air for ios的拍照和保存图片到相册功能,主要用到两个API:CameraUI 和 CameraRoll。前者是负责调用系统的相机拍照的,对其侦听 MediaEvent.COMPLETE 事件,会得到MediaPromise对象。这个对象还可以通过CameraRoll选择相册图片得到。得到这个后,通过loader,得到bitmap对象。然后调用CameraRoll,将其保存到相册里边。这里要说的不是这两个API。还是说些我知道的,大家也知道的吧。CameraRoll很二,浏览选择图片的时候,一次只能选择一张。而且CameraRoll浏览选择图片,得不到图片的路径。这两个API,方法和属性都很少。对于在手机上开发,还有个很判断,都习惯判断设备是否支持该操作。如:if(CameraUI.isSupported){}。最后说一点,相机拍照后,怎么将相片完美的显示到舞台呢。设备设置为自动感应重力方向,会旋转。无论怎么旋转,图片的宽度永远大于图片的宽度。就是bitmap的宽度高度是固定的了。而舞台的大小却在变化着。还有方向也在变化着。这就导致addChild舞台的时候,方向,位置,大小不一的问题。这里的源头是找不到手机的重力方向状态。比如是否是正竖着方向,是否是正横着方向,是否是反横着方向这些。估计有API知道这个。由于对ios开发还不熟悉,还在研究中。如果知道了这些也好控制图片的位置,大小,方向问题。
要洗澡澡睡觉觉了。在这之前,还是把代码贴出来。
文档类:
package { import com.vini123.camera.Camera; import com.vini123.utils.Button; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; public class PhotoCamera extends Sprite { private var camera:Camera; private var photoBtn:Button; private var saveBtn:Button; public function PhotoCamera() { super(); // 支持 autoOrient stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; camera = new Camera(); addChild(camera); photoBtn = new Button("拍照"); photoBtn.name = "photoBtn"; photoBtn.buttonMode = true; photoBtn.addEventListener(MouseEvent.CLICK,clickHandler); photoBtn.x = 10; photoBtn.y = 10; addChild(photoBtn); saveBtn = new Button("保存"); saveBtn.name = "saveBtn"; saveBtn.buttonMode = true; saveBtn.addEventListener(MouseEvent.CLICK,clickHandler); saveBtn.x = 15 + photoBtn.width + photoBtn.x; saveBtn.y = 10 ; addChild(saveBtn); } private function clickHandler(e:MouseEvent):void { var tarName:String = e.currentTarget.name; trace(tarName); switch(tarName) { case "photoBtn": camera.doCamera(); break; case "saveBtn": camera.doSave(); break; } } } }
Camera 类:
package com.vini123.camera { import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MediaEvent; import flash.media.CameraRoll; import flash.media.CameraUI; import flash.media.MediaPromise; import flash.media.MediaType; public class Camera extends Sprite { private var _cameraUi:CameraUI; private var _cameraRoll:CameraRoll; private var _bitmap:Bitmap; public function Camera() { _cameraUi = new CameraUI(); _cameraUi.addEventListener(MediaEvent.COMPLETE,mediaCompleteHandler); _cameraRoll = new CameraRoll(); _cameraRoll.addEventListener(Event.COMPLETE,cameraRollCompleteHandler); } public function doCamera():void { if(CameraUI.isSupported) { _cameraUi.launch(MediaType.IMAGE); trace("open"); } } public function doSave():void { if(CameraRoll.supportsAddBitmapData) { _cameraRoll.addBitmapData(_bitmap.bitmapData); } } private function showPhoto():void { //无论是横着拍的照片还是竖着拍的照片。照片的宽度永远大于高度。就是一个规格了。而舞台的宽度高度会改变。 trace(stage.stageWidth,stage.stageHeight,_bitmap.width,_bitmap.height); if(stage.stageWidth > stage.stageHeight) { _bitmap.width = stage.stageWidth; _bitmap.scaleY = _bitmap.scaleX; } else { _bitmap.width = stage.stageHeight; _bitmap.scaleY = _bitmap.scaleX; _bitmap.rotation = 90; _bitmap.x = _bitmap.width; } addChild(_bitmap); } private function mediaCompleteHandler(e:MediaEvent):void { var mediaPromise:MediaPromise = e.data; var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleteHandler); loader.loadFilePromise(mediaPromise); } private function loadCompleteHandler(e:Event):void { trace("loadComplete"); e.target.removeEventListener(Event.COMPLETE,loadCompleteHandler); _bitmap = e.target.content as Bitmap; showPhoto(); } private function cameraRollCompleteHandler(e:Event):void { trace("rollComplete"); } } }
Button类:有人说喜欢蓝色,就先用这个蓝色底吧。
package com.vini123.utils { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class Button extends Sprite { private var _txtFormat:TextFormat; private var _txt:TextField private var _bottom:Sprite; private var _gap:int =15; public function Button(label:String):void { _txtFormat = new TextFormat(); _txtFormat.size = 30; _txtFormat.color = 0xADD0FC; _txt = new TextField(); _txt.text = label; _txt.autoSize = TextFieldAutoSize.LEFT; _txt.setTextFormat(_txtFormat); _txt.mouseEnabled = false; addChild(_txt); _bottom = new Sprite(); _bottom.graphics.lineStyle(1.0,0x2D72B5); _bottom.graphics.beginFill(0x0099ff,1); _bottom.graphics.drawRoundRect(0,0,_txt.textWidth + _gap *2,_txt.textHeight + _gap,5,5); _bottom.graphics.endFill(); addChildAt(_bottom,0); _txt.x = _gap; _txt.y = _gap * 0.5 -1; this.addEventListener(MouseEvent.ROLL_OVER,overHandler); this.addEventListener(MouseEvent.ROLL_OUT,outHandler); } private function overHandler(e:MouseEvent):void { _txt.alpha = 0.65; } private function outHandler(e:MouseEvent):void { _txt.alpha = 1; } public function dispose():void { _bottom.graphics.clear(); this.removeEventListener(MouseEvent.ROLL_OVER,overHandler); this.removeEventListener(MouseEvent.ROLL_OUT,outHandler); if(this.parent) { this.parent.removeChild(this); } } } }
这些都是在家里做的。公司的iphone,公司的开发者账号。