0

In my iOS app, i have to deal with some big images, specifically the size may be between 1MB and 30MB.

Now i have to convert the big images down to around 400KB (380~400) with the minimum loss of quality.

We have some solution to convert it into a particular dimension (CGSize), but I don't know how to convert the image to some other size on disk...

Wain
  • 118,658
  • 15
  • 128
  • 151
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
  • 1
    'without losing quality' - how do you propose that could work??? – Wain Jun 03 '16 at 06:46
  • Without losing means we should try our best to keep the maximum quality. Yes, you are right. @Wain – Jamshed Alam Jun 03 '16 at 06:58
  • @Wain, I have followed this link. But here i get random memory size. But i need it in a range. http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage – Jamshed Alam Jun 03 '16 at 07:05
  • 1
    its a bit difficult to get a specific file size when reducing the size of the image, because the png or jpeg compression algorithm is a bit of a black box and you wont know the size of the file until the image has been compressed, so you might just have to do like a divide and conquer approach to finding the dimensions of the image to get the size of the file you need – Fonix Jun 03 '16 at 07:13
  • I messed up in that way. Fonix – Jamshed Alam Jun 03 '16 at 08:30
  • @Wain, thanks for your edit, but try to help me. – Jamshed Alam Jun 03 '16 at 09:00
  • please be patient, people have other things to do with their time also... – Wain Jun 03 '16 at 09:05

2 Answers2

1

The easiest way to do this is with the JPEG image format because when you convert the image to data you can specify a simple compression quality value. This is done using UIImageJPEGRepresentation.

The other part to this is that you don't know the size until the operation is complete. So, you need to test the result and decide what to do.

Obviously you can optimise and check if the original image is already small enough. If it isn't then you can run a binary search to locate the appropriate compression quality setting.

A binary search means always halving your distance between the current and the goal state. So, the compression quality is somewhere between 0 and 1, which means you start at a compression quality of 0.5 and see what the result is. If the image is still too big you discard the data and try again with a compression quality of 0.25 (0.5 - (0.5 / 2)). If the image is too small u discard the data and try again with a compression quality of 0.75 (0.5 + (0.5 / 2)).

Keep doing that until the image size is in your acceptable range.

Wain
  • 118,658
  • 15
  • 128
  • 151
0
+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newsizeimage
{

    CGSize actualSize = image.size;

float scale = actualSize.width/actualSize.height;

if (scale < 1) {
    newsizeimage.height = newsizeimage.width/scale;
} 
else {
    newsizeimage.width = newsizeimage.height*scale;
}

UIGraphicsBeginImageContext(newsizeimage);
[image drawInRect:CGRectMake(0, 0, newsizeimage.width, newsizeimage.height)];
UIImage* mynewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return mynewImage;

}

UPDATE

For Reducing image Size:

Step 1:

Save the image in jpeg format for reduction of image size like this:

 NSData *imageData = UIImageJPEGRepresentation(your UIImage to save, 0.5);


[imageData writeToFile:path of doc_dir with image name atomically:YES];

Step 2

For reduce the quality and size of the image use this :

CGSize newSize=CGSizeMake(50,50); // you can change resolution as pe your need


  UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];


UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19