{"id":831,"date":"2013-01-02T12:19:25","date_gmt":"2013-01-02T11:19:25","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=831"},"modified":"2013-01-02T12:20:07","modified_gmt":"2013-01-02T11:20:07","slug":"take-photo-with-air-on-ios","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/take-photo-with-air-on-ios\/","title":{"rendered":"Take photo with AIR on iOS"},"content":{"rendered":"<p>Hey guys,<\/p>\n<p>Recently, I&#8217;ve worked on a mobile project and camera use drives me crazy about performances. On iOS, after taking a photo and validate it, it freezes the application during one minute to be able to encode it.<br \/>\nI&#8217;ve made lots of tests with several libraries, using Alchemy ones, and finally the fastest way (less than 5 seconds) is to use the new BitmapData <a href=\"http:\/\/help.adobe.com\/en_US\/FlashPlatform\/reference\/actionscript\/3\/flash\/display\/BitmapData.html#encode%28%29\" target=\"_blank\">encode<\/a> method, available since Flash Player 11.3 and AIR 3.3.<\/p>\n<p>Here is the code (lots of code come from this <a href=\"http:\/\/stackoverflow.com\/questions\/11812807\/take-photo-using-adobe-builder-flex-for-ios\" target=\"_blank\">excellent post<\/a>) :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">if (CameraUI.isSupported) {\r\n\tviewsAC.addItem({label:\"Take Photo\", icon:cameraIcon});\r\n\tmyCam = new CameraUI();\r\n\tmyCam.addEventListener(MediaEvent.COMPLETE, onCameraComplete);\r\n}\r\n\r\nprotected function onCameraComplete(evt:MediaEvent):void\r\n{\r\n\tsqlStatement = new SQLStatement();\r\n\tsqlStatement.sqlConnection = model2.connection;\r\n\tsqlStatement.text =\r\n\t\t\"INSERT INTO albumItems (album, photoFile, dateAdded) \" +\r\n\t\t\"VALUES (:album, :photoFile, :dateAdded)\";\r\n\tsqlStatement.parameters[\":album\"] = model2.selectedAlbum;\r\n\t\r\n\tvar mediaPromise:MediaPromise = evt.data;\r\n\tif (mediaPromise.file == null) {\r\n\t\t\r\n\t\t\/\/ For iOS we need to load with a Loader first\r\n\t\t_loader = new Loader();\r\n\t\t_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageloader_complete, false, 0, true);\r\n\t\t_loader.loadFilePromise(mediaPromise);\r\n\t\t\r\n\t\treturn;\r\n\t} else {\r\n\t\t\r\n\t\t\/\/ Android & BlackBerry\r\n\t\tregisterPhoto(mediaPromise.file.url)\r\n\t}\r\n}\r\n\r\nprivate function imageloader_complete(event:Event):void {\r\n\t\r\n\t_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageloader_complete);\r\n\r\n\tvar loaderInfo:LoaderInfo = event.target as LoaderInfo;\r\n\r\n\tif (CameraRoll.supportsAddBitmapData) {\r\n\r\n\t\tvar bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height);\r\n\t\tbitmapData.draw(loaderInfo.loader);\r\n\t\tvar file:File = File.applicationStorageDirectory.resolvePath(\"image\" + new Date().time + \".jpg\");\r\n\t\tvar stream:FileStream = new FileStream()\r\n\t\tstream.open(file, FileMode.WRITE);\r\n\t\tvar bytes:ByteArray = bitmapData.encode(bitmapData.rect, new flash.display.JPEGEncoderOptions());\r\n\t\tstream.writeBytes(bytes, 0, bytes.bytesAvailable);\r\n\t\tstream.close();\r\n\t\t\r\n\t}\r\n\tregisterPhoto(file.url)\r\n\t\t\r\n}\r\n\r\nprivate function registerPhoto(url:String):void {\r\n\r\n\tsqlStatement.parameters[\":photoFile\"] = url;\r\n\t\t\r\n\tsqlStatement.parameters[\":dateAdded\"] = new Date();\r\n\tsqlStatement.execute();\r\n\t\r\n\tviewsList.selectedIndex = -1;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey guys, Recently, I&#8217;ve worked on a mobile project and camera use drives me crazy about performances. On iOS, after taking a photo and validate it, it freezes the application during one minute to be able to encode it. I&#8217;ve made lots of tests with several libraries, using Alchemy ones, and finally the fastest way &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/take-photo-with-air-on-ios\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Take photo with AIR on iOS<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4,33],"tags":[119,15,34],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/831"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":3,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":834,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions\/834"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}