0

**我有一个我一直在开发的 iOS 空气应用程序。我正在尝试捕获图片,将文件保存到存储目录(不是相机胶卷),并将文件名保存在 sqlite db 中。

我已经尝试了很多不同的变体,但是在编写文件流以保存应用程序时会挂起。在 iPad 3 上进行测试。有人有什么建议吗?这几天让我发疯了。我已经在网上搜索过,但我很难过。**

      public var temp:File; // File Object to save name in database
        protected function selectPicture():void
        {
            
            myCam = new CameraUI();
            myCam.addEventListener(MediaEvent.COMPLETE, onComplete);
        
            myCam.launch(MediaType.IMAGE);
        }
        
        
        protected function onComplete(event:MediaEvent):void {
        
        //imageProblem.source = event.data.file.url;
        
            var cameraUI:CameraUI = event.target as CameraUI;
            
            
            var mediaPromise:MediaPromise = event.data;
            
            var mpLoader:Loader = new Loader();
            mpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMediaPromiseLoaded);
            
            mpLoader.loadFilePromise(mediaPromise);
            

        }
        
        private function onMediaPromiseLoaded(e:Event):void
        {
            var mpLoaderInfo:LoaderInfo = e.target as LoaderInfo;
            mpLoaderInfo.removeEventListener(Event.COMPLETE, onMediaPromiseLoaded);
            
            this.imageProblem.source = mpLoaderInfo.loader;
            
        
            var stream:FileStream = new FileStream();
            
            stream.addEventListener(Event.COMPLETE, showComplete);
            stream.addEventListener(IOErrorEvent.IO_ERROR, showError);
            
            
            try{
                
            this.messages.text = "Starting";
                
            stream.open( temp, FileMode.WRITE );
            stream.writeBytes(mpLoaderInfo.bytes);
            stream.close();
            
            }catch(e:Error){
            
                this.messages.text = e.message;
            
            }
            
            
        
        
        
        }
        
        protected function showError(e:IOErrorEvent):void{
            
            this.messages.text = e.toString();
            
            
        }
        
        protected function showComplete(e:Event):void{
            
            this.messages.text = "Completed Writing";
            this.imgName.text = temp.url;
            imagefile = temp;
            deleteFlag = 1;
        }
4

2 回答 2

0

由于您试图在Sync mode.

您需要使用Async Mode操作而不是同步模式文件操作。

stream.openAsync( temp, FileMode.WRITE );

试试这个

var stream:FileStream = new FileStream();
stream.addEventListener(Event.COMPLETE, showComplete);
stream.addEventListener(IOErrorEvent.IO_ERROR, showError);
stream.openAsync( temp, FileMode.WRITE );
stream.writeBytes(mpLoaderInfo.bytes);
stream.close();

请注意,当使用异步操作时,您不需要使用try catch.For 处理错误监听 IOErrorEvent 将在发生任何错误时捕获。

于 2013-02-25T05:00:30.837 回答
0

我终于让它工作了,我在代码下面添加了注释来解释它为什么不起作用。

                    public var temp:File;
        protected function selectPicture():void
        {

            myCam = new CameraUI();
            myCam.addEventListener(MediaEvent.COMPLETE, onComplete);

            myCam.launch(MediaType.IMAGE);
        }


        protected function onComplete(event:MediaEvent):void {

        //imageProblem.source = event.data.file.url;

            var cameraUI:CameraUI = event.target as CameraUI;


            var mediaPromise:MediaPromise = event.data;

            var mpLoader:Loader = new Loader();
            mpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMediaPromiseLoaded);

            mpLoader.loadFilePromise(mediaPromise);


        }

        private function onMediaPromiseLoaded(e:Event):void
        {
            var mpLoaderInfo:LoaderInfo = e.target as LoaderInfo;
            mpLoaderInfo.removeEventListener(Event.COMPLETE, onMediaPromiseLoaded);

            this.imageProblem.source = mpLoaderInfo.loader;




                /// Here was the solution

                var bitmapDataA:BitmapData = new BitmapData(mpLoaderInfo.width, mpLoaderInfo.height);
                bitmapDataA.draw(mpLoaderInfo.content,null,null,null,null,true);  

                /// I had to cast the loaderInfo as BitmapData 

                var bitmapDataB:BitmapData = resizeimage(bitmapDataA, int(mpLoaderInfo.width / 4), int(mpLoaderInfo.height/ 4));  // function to shrink the image





                var c:CameraRoll = new CameraRoll();
                c.addBitmapData(bitmapDataB);

                var now:Date = new Date();
                var f:File = File.applicationStorageDirectory.resolvePath("IMG" + now.seconds + now.minutes + ".jpg");                                    
                var stream:FileStream = new FileStream()
                stream.open(f, FileMode.WRITE);                                         

                // Then had to redraw and encode as a jpeg before writing the file


                var bytes:ByteArray = new ByteArray();
                bytes = bitmapDataB.encode(new Rectangle(0,0, int(mpLoaderInfo.width / 4) , int(mpLoaderInfo.height / 4)), new JPEGEncoderOptions(80), bytes);




                stream.writeBytes(bytes,0,bytes.bytesAvailable);
                stream.close(); 

                this.imgName.text = f.url;


            imagefile = f;
            deleteFlag = 1;

        }
于 2013-02-25T14:22:13.100 回答