sagarkothari
sagarkothari

Reputation: 24820

downSize image from iPhone while uploading

I have an application, in which

User selects an images from iPhone, for photo field,

Image is uploaded to the server,

Image size may be of 1 kb, or 10 kb or it may be 1 mb or 10 mb.

If image size is more than 5mb than image must be downsized upto 5mb before storing to the server.Question is How?is it Possible?

i have used following code for downloading image - for compressing image while loading,
imageDatafromPhoto = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],0.2);

Thanks for helping me.

Upvotes: 1

Views: 487

Answers (1)

AlBlue
AlBlue

Reputation: 24060

There's a difference between 'compressing' and 'resampling'. Compression is a lossless way of making a document smaller, such as GZip. Although there aren't any standard interfaces in Objective-C to help you do this, there's a few extensions for NSData as a category that you can use to add GZip compression.

However, that in itself won't help you. JPEG images tend not to compress well, so what you really want is resampling or adjustment of the image itself. This throws some data away, making it smaller but also worse quality. This is the purpose of the 0.2 at the end of it - 0.0 is the maximal compression that you can get out of the library. You could try calling it with ever successively small numbers (say, in a for loop) until you get an image size that is of acceptable quality.

Failing that, you would have to do a resize by drawing the image into a CGContextDrawImage with a specified size into a CG buffer (from CGBitmapContextCreate) and then get the new image from the result drawn in there, and start again.

Upvotes: 1

Related Questions