Skip to main content
Inspiring
Mar 14, 2014 4:54 AM UTC
Question

Convert iPhone photo to JPG before upload to server?

The issue I'm having is that I'm trying to have the user upload a photo from their iPhone to my server. However, I don't want their raw, large file size photo. Is there a way I can encode the photo to a smaller quality JPG before the user uploads it?

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

Colin Holgate
Inspiring
Mar 14, 2014 5:05 AM UTC

Yes, read about JPEGEncoderOptions:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/JPEGEncoderOptions.html

Translate
Inspiring
Mar 14, 2014 5:29 AM UTC

Thanks for the quick reply, Colin. The problem I'm having is how do I use the JPEGEncoder when the photo data is in a ByteArray?

// Create a new imagePromise

               var imagePromise:MediaPromise = evt.data;

// Open our data source

               dataSource = imagePromise.open();

               var imageBytes:ByteArray = new ByteArray();

                              dataSource.readBytes( imageBytes );

// Create a new FileStream

               var stream:FileStream = new FileStream();

               stream.open(temp, FileMode.WRITE);

               stream.writeBytes(imageBytes);

               stream.close();

Translate
Colin Holgate
Inspiring
Mar 14, 2014 12:14 PM UTC

You can get the bitmap from the media promise with this:

var imageB = imageLoader.content as Bitmap;

This gives you the bitmapdata and encodes it as a jPEG ByteArray

var imageBD:BitmapData = imageB.bitmapData;

var byteArray:ByteArray = new ByteArray();

imageBD.encode(new Rectangle(0,0,imageBD.width, imageBD.height), new JPEGEncoderOptions(), byteArray);

‘byteArray’ now is the JPEG ready to upload. The whole upload side of things is its own issue, but I’ll wait to see if you have questions on that!

Translate