Skip to main content
Inspiring
March 14, 2014
Question

Convert iPhone photo to JPG before upload to server?

  • March 14, 2014
  • 1 reply
  • 5873 views

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 has been closed for replies.

1 reply

Colin Holgate
Inspiring
March 14, 2014

Yes, read about JPEGEncoderOptions:

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

Inspiring
March 14, 2014

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();

Colin Holgate
Inspiring
March 14, 2014

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!