Reduce file size of UIImage? - iphone

I want to keep a UIImage the same viewable size but reduce it's file size. Is there a way to do this?
For example, if the user is saving 10 images taken with the camera, i'd like them to come to a smaller file size while keeping most of the quality and the same width and height of the original image.

save as jpeg with higher compression
(will reduce quality)
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);

Related

How to reduce image size captured from cam

The Picture taken from the iphone cam is nearly 2.5 Mb, How to reduce this size ,I have tried
UIJPEGRepresentation(image,0.1f),but it does not effect the size ?
You really can't reduce the size the images takes up in memory.
When an image is loaded, basically a UIImage object the size wil be width x height x 4 bytes. That is the size the an uncompressed image will take up in memory.
Since you can use compressed images all image, once loaded in a UIImage will be uncompressed.
If you really need so save some memory, save the image to disk and create a thumbnail which you use in your app. Then when need you can load the larger image and use it,
Try using the Resize method in UIImage+Resize.h
https://github.com/AliSoftware/UIImage-Resize
[aImgView setImage:[ImageObjectFromPicker resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:YourSize interpolationQuality:kCGInterpolationHigh]];

objective c how to change image quality?

I've googled a lot about this question, but couldn't find anything interesting for me.
Q: Is there any class or method to change image quality(not size or scale, but the quality keeping the same size and scale).
As I understand there is no native(default) classes or methods to do this, am I right?
Any help would be appreciated.
If you have an image as a UIImage, you can use the UIImageJPEGRepresentation function, specifying a compression quality,to create an NSData object. This data object can then be used to create a new UIImage.
See the Apple Docs
This functionality is already available in iOS SDK itself to set image quality.
In the UIImage class, there is one method UIImageJPEGRepresentation.
In this function pass your image and image compression quality. It will not change dimension of the image but it will change the dip of the image to maintain the quality. It will help to maintain the size in memory as well.
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
Parameters:
- image : The original image data.
- compressionQuality: The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).
Hope this is what you required.
Enjoy Coding :)

size of an image

From times to times I have to know the width and height of images. I am using the following code:
UIImage *imageU = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myImage.png"]];
CGFloat imageW = CGImageGetWidth(imageU.CGImage);
CGFloat imageH = CGImageGetHeight(imageU.CGImage);
My question is that if it is there any other way to know the width and height of an image, in pixels, without having to load the image on a variable, that's probably consuming memory. Can the dimensions be read from the file directly without loading the whole image?
This has to work for PNG and JPEG.
thanks.
You can parse the PNG file which contain the size info in the header. See Get size of image without loading in to memory.
(BTW, you can use imageU.size to get a CGSize of the image.)
I don't think you can do this "for free", speaking in terms of memory use. You could create an NSInputStream and read/parse the data of the IHDR section (follow #KennyTM's link), then discard everything.

How to reduce UIImage size to a maximum as possible

I am using following code to resize the image.
Resize a UIImage Right Way
And I use interpolation quality as kCGInterpolationLow.
And then I use UIImageJPEGRepresentation(image,0.0) to get the NSData of that image.
Still its a little bit high in size around 100kb. when I send it over the network. Can I reduce it further. If I am to reduce it more what could I do ?
Thanks and Kind Regards,
you image compress and image data stroed NSData format. The function is
UIImageJPEGRepresentation(UIImage * _Nonnull image, CGFloat compressionQuality);
Example:
NSData *objImgData = UIImageJPEGRepresentation(objImg,1.0);

Iphone reduce Image file size

Is there anyway to reduce the Image file size or Raw RGB buffer ?
Actually I have RGB buffer which it has 500KB with 320X420 size.I tried to save it to disk using UIimage and it comes to 240 KB.
As per the image size, I want it to have less than 50KB or so.(loosing quality is OK)
Is it possible ?
Thanks,
Raghu
See Trevor Harmon's excellent post on the subject.
The 240KB size sounds exactly like the raw RGB image data, uncompressed (320x420x3). Doesn't the iPhone have a PNG or JPEG exporter? The internet says to use UIImageJPEGRepresentation or UIImagePNGRepresentation and NSData writeToFile.