AIR 3.0 – CameraUI.browseForImage on iOS: Fixing “null” file issue

If you have every tried to port an AIR app to iOS, that makes use of the CameraRoll.browseForImage() API, you will run into a strange issue.

On Android, the complete handler has an event.data.file property which points to the location of the photo that was just taken. However, on iOS, no file is actually created, iOS simply stores the image in memory, putting the responsibility on you to write the bitmapData.

So, what do we do if we can’t access the file location? Use the file promise instead. This method works requires one additional step, but it works on all platforms.

The event.data property is a media promise, and we can use the Loader() API to load the promise, and retrieve the bitmapData object. Once we have the bitmapData, it’s trivial to call CameraRoll.addBitmapData() and save it to the device.

Here’s an example of loading a camera image on iOS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
protected function takePhoto():void {
	var camera:CameraRoll = new CameraRoll();
	camera.browseForImage();
	camera.addEventListener(MediaEvent.SELECT, onPhotoSelected);
}
 
protected function onPhotoSelected(event:MediaEvent):void {
        //Get file promise
	var imagePromise:MediaPromise = event.data;
        //Load bitmapData
	var loader:Loader = new Loader();
	loader.contentLoaderInfo.addEventListener(Event.INIT, onPhotoBitmapLoadComplete, false, 0, true);
	loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, photoFailed, false, 0, true);
	loader.loadFilePromise(imagePromise);
}
 
protected function onPhotoBitmapLoadComplete(event:Event):void {
        //And we have data!
	var bitmapData:BitmapData = (event.target.content as Bitmap).bitmapData;
}

Written by

No Comments Yet.

Leave a Reply

Message