Question

I have doubts about the resizing of images in my iphone app. So the user takes a picture or choses one from the gallery and then this pic is sent to a server. I already have the code from this answer to resize the pic:

UIImage * small=[self imageByScalingToSize:CGSizeMake(150,150) img:image];

The size is just an example.

I send small to the server but I have to things to take into account:

  • The image has to be less or equal to 150 kilobytes, and
  • I have read here that when the user sends images over 3G the iphone automatically resizes the image to (800,600).

So what I would like to do is to resize image to (800,600) (this part I already have) and then check if it is more than 150k and somehow reduce it if it is.

Is there a way to calculate the number of bytes of the image knowing the size and maybe some other data ?
How could I achieve this size reduction in means of file size ?

Was it helpful?

Solution

convert your UIImage to NSData and user -length to get the byte size of the image.

from apple documentation:

length - Returns the number of bytes contained in the receiver.

you can convert the UIImage using either

NSData * UIImagePNGRepresentation ( UIImage *image );

or

NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality );

OTHER TIPS

How are you uploading you image. You should just be sending up the byte data, iOS will not know that it's an image and will not be able to resize it! Have you looked at using something like

NSData *imageData = UIImageJPEGRepresentation(myUIImage, compressionQuality);

To compress your image as well as resize it. This will give you an NSData object which you can the base64 encode and send you to your server. Also form the NSData you will be bale to see how big the the image is in bytes and determine if it is small enough.

Hope this helps. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top