compress UIImage but keep size - ios

I try to use UIImageView to show the photo. But the Photo sometimes is a little large, and I want to compress it.But I'd like to keep its size.
For example,a photo is 4M and has a size of 320X480. And I want to compress it and it may have 1M but still has a size of 320X480.
thanks!

Compress it using the JPEG compression.
lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(highResImage, quality)];
Where quality is between 0.0 and 1.0
You should read the UIImage documentation, everything is explained in there…

If your goal is to get the image below a specific data length, it's tough to guess what compression ratio you need, unless you know the source image will always be a certain data length. Here's a simple iterative approach that uses jpeg compression to achieve a target length... let's say 1MB, to match the question:
// sourceImage is whatever image you're starting with
NSData *imageData = [[NSData alloc] init];
for (float compression = 1.0; compression >= 0.0; compression -= .1) {
imageData = UIImageJPEGRepresentation(sourceImage, compression);
NSInteger imageLength = imageData.length;
if (imageLength < 1000000) {
break;
}
}
UIImage *finalImage = [UIImage imageWithData:imageData];
I've seen some approaches that use a while loop to compresses the image by .9 or whatever until the target size is met, but I think you'd be losing image quality and processor cycles by successively compressing/reconstituting the image. Also, the for loop here is a little safer because it stops automatically after trying the maximum possible compression (zero).

Related

the memory of using drawInRect to resize picture

recently i use PHAssets replace the old Assets in my project.However,when i use my app to scale some pictures ,i found it usually crashes.
i use the debug mode, found it is the memory problem.
i use the code below to resize picture
+(UIImage*)scaleRetangleToFitLen:(UIImage *)img sWidth:(float)wid sHeight:(float)hei{
CGSize sb = img.size;;
if (img.size.height/img.size.width > hei/wid) {
sb = CGSizeMake(wid,wid*img.size.height/img.size.width);
}else{
sb = CGSizeMake(img.size.width*hei/img.size.height,hei);
}
if (sb.width > img.size.width || sb.height > img.size.height) {
sb = img.size;
}
UIImage* scaledImage = nil;
UIGraphicsBeginImageContext(sb);
[img drawInRect:CGRectMake(0,0, sb.width, sb.height)];
scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img = nil;
return scaledImage;
}
the memory will increase about 50M when the code
[img drawInRect:CGRectMake(0,0, sb.width, sb.height)]
runs and it will not be setted free even thought the method is finished.
the width and the height is 304*228 ,the image original size is about 3264*2448,the returned image is 304*228;it means the real image i wanted at last is just a 304*228 size image,however it takes 50+M memory..
Is there any way to free the memory the drawInRect: function takes?
(the #autoreleasepool does not work ~ 😢 😢)
When loading an image, iOS usually doesn't decompress it until it really needs to. So the image you pass into your function is most likely a JPEG or PNG image that iOS keeps in memory in it's compressed state. The moment you draw it, it will be decompressed first and therefore the memory increases significantly. I would expect an increase by 3264 x 2448 x 4 = 35MB (and not 50MB).
To get rid of the memory again, you will make sure you release all reference to the image you pass into your function. So the problem is outside the code you show in your question.
For a more specific answer, you'll need to show all the code that works with the original image.

How to compress images in iOS?

I have been looking through every stack overflow post and video online and I can't find a solution that works correctly. Right now the user selects a photo that I want to compress before uploading to an AWS S3 bucket. The upload works perfectly but for some reason, the compressed image is larger than the original image! For example, if the user selects a 9KB photo, when I upload to S3 the photo is 28.5KB. I tried a different photo and it's 48KB and after "compression" on S3 its 378.9KB! (I am using the latest software version of everything and compiling with the simulator)
I want to compress the original image as much as I can before uploading.
This is what I have so far:
How I "compress" the image:
UIImage *compressedProductImage;
NSData *NSproductImage;
NSUInteger productImageSize;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
self.productImage = info[UIImagePickerControllerOriginalImage];
[self.productImageImageView setImage:self.productImage];
[self dismissViewControllerAnimated:YES completion:nil];
NSproductImage = UIImageJPEGRepresentation(self.productImage, 0.5f);
productImageSize = [NSproductImage length];
compressedProductImage = [UIImage imageWithData: NSproductImage];
How I upload the photo:
//Convert product UIImage
NSArray *productImagePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *productImageFilePath = [[productImagePaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#".png"]];
[UIImagePNGRepresentation(compressedProductImage) writeToFile:productImageFilePath atomically:YES];
NSURL *productImageFileUrl = [NSURL fileURLWithPath:productImageFilePath];
uploadRequest.body = productImageFileUrl;//Needs to be a NSURL
uploadRequest.bucket = AWS_BUCKET_NAME;
uploadRequest.key = productImageKey;
uploadRequest.contentType = #"image/png";
uploadRequest.ACL = AWSS3BucketCannedACLPublicRead;
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
if (task.error != nil) {
NSLog(#"%s %#","Error uploading (product image):", uploadRequest.key);
}else{
NSLog(#"Product image upload completed");
}
return nil;
}];
As rmaddy points out, you're taking the picked image, converting it to JPEG, converting back to a UIImage (losing any benefit of the JPEG compression), and then converting it to a PNG, which offers modest compression, generally far less compression than the original JPEG from the users photo library.
You have a few options.
You can retrieve the original imageData from the asset in your photos library as shown in https://stackoverflow.com/a/32845656/1271826, thereby avoiding the round-tripping it through a UIImage at all. Thus, you preserve the quality of the original image, preserve the meta data associated with this image, and enjoy the decent compression of the original asset.
You could take the picked image as a UIImage and do a combination of:
reduce the dimensions of the image before you call UIImageJPEGRepresentation (see https://stackoverflow.com/a/10491692/1271826 for sample algorithm); and/or
use UIImageJPEGRepresentation with a quality less than 1.0, where the smaller the number, the more compression but the more image quality loss.
You don't actually compress anything. You start with a UIImage. This is a full pixel by pixel representation that takes (typically) width x height x 4 bytes.
You then convert that to a JPG with some compression. So NSproductImage is a much smaller representation, in memory, of the JPG version of the image. This is the supposed smaller size you see where you think it is now compressed.
But then you convert that JPG data back into a UIImage as compressedProductImage. This new UIImage still has the same width and heigh of the original UIImage. As a result, it still takes the same width x height x 4 bytes as the original. It's just of lower quality than the original due to the JPG compression.
Now you convert the updated UIImage into a PNG. Since PNG is lossless, it doesn't compress nearly as much as the JPG attempt. You then send this larger PNG version of the image to Amazon.
You should first remove the pointless code that first converts to JPG and then back to UIImage.
At this point you should either live with the size of the PNG or use JPG instead and send the smaller JPG to Amazon.
Another option would be to scale the image before sending it to Amazon.

How to compress image size using UIImagePNGRepresentation - iOS?

I'm using UIImagePNGRepresentation to save an image. The result image is of size 30+ KB and this is BIG in my case.
I tried using UIImageJPEGRepresentation and it allows to compress image, so image saves in < 5KB size, which is great, but saving it in JPEG gives it white background, which i don't want (my image is circular, so I need to save it with transparent background).
How can I compress image size, using UIImagePNGRepresentation?
PNG uses lossless compression, that's why UIImagePNGRepresentation does not accept compressionQuality parameter like UIImageJPEGRepresentation does. You might get a bit smaller PNG file with different tools, but nothing like with JPEG.
May be this will help you out:
- (void)resizeImage:(UIImage*)image{
NSData *finalData = nil;
NSData *unscaledData = UIImagePNGRepresentation(image);
if (unscaledData.length > 5000.0f ) {
//if image size is greater than 5KB dividing its height and width maintaining proportions
UIImage *scaledImage = [self imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2];
finalData = UIImagePNGRepresentation(scaledImage);
if (finalData.length > 5000.0f ) {
[self resizeImage:scaledImage];
}
//scaled image will be your final image
}
}
Resizing image
- (UIImage*)imageWithImage:(UIImage*)image andWidth:(CGFloat)width andHeight:(CGFloat)height
{
UIGraphicsBeginImageContext( CGSizeMake(width, height));
[image drawInRect:CGRectMake(0,0,width,height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext() ;
return newImage;
}

Scale an image to a given NSData byte size - PNG compression [duplicate]

This question already has answers here:
How to downscale a UIImage in IOS by the Data size
(5 answers)
Closed 8 years ago.
If I have a UIImage and I convert is to NSData I can see how many bytes it is.
If I have a variable requiredSize and I want to set that UIImage to a certain length and width so that when it is rendered as a PNG-file NSData UIImagePNGRepresentation(); it is a certain byte-size (requiredSize). How do I go about doing this.
I know how to get the current byte size [NSData length];
And I know how to downscale a UIImage (If there's a better way please tell me)
//UIImage *tempImage = whateverTheImagePointerIs;
int tempWidth = tempImage.size.width/2;//50% width of original
int tempHeight = tempImage.size.height/2;//50% height of original
UIImageView *tempImageRender = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tempWidth, tempHeight)];
tempImageRender.image = tempImage;
UIGraphicsBeginImageContextWithOptions(tempImageRender.bounds.size, tempImageRender.opaque, 1.0);
[tempImageRender.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tempFinalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But when I scale it by 50% on width and 50% on height (25% net-total) the final bytes of the new rendered scaled image (when converted to PNG using UIImagePNGRepresentation();) is not 25% of the original bytes... it seems to just be random (I'm sure this is happening because PNG compression algorithms change with image quality/size.)
Is there no way to resize an image to a given byte size?
See this answer for how to scale an image fairly effectively:
As you've discovered, there really isn't a direct correlation between image size and data size when compressed, you'll just have to do it iteratively:
-(NSData*)pngRepresentationWithMaxSize:(NSInteger)maxSize
{
UIImage* image = self;
while(1)
{
NSData* data = UIImagePNGRepresentation(image);
if(data.length < maxSize)
return data;
CGSize size = image.size;
image = [UIImage imageScaledToSize:CGSizeMake(image.size.width / 2., image.size.height / 2.)];
}
return nil;
}
Note that I assume you change the referenced scaling code to be a category method on UIImage and put the method above into a UIImage category as well.

Compressing UIImage into JPEG image data multiple times

I am debugging a piece of code where an UIImage may be gone through UIImageJPEGRepresentation multiple times, I thought that must be a bug and the image quality will get worsen, but surprisingly we can't see the difference visually.
So I did a test, loading an image, and try to let it go through UIImageJPEGRepresentation 1000 times, surprisingly, whether 1 or 1000 times doesn't really make a difference in the image quality visually, why is that so?
This is the testing code:
UIImage *image = [UIImage imageNamed:#"photo.jpeg"];
// Create a data reference here for the for loop later
// First JPEG compression here
// I would imagine the data here already has low image quality
NSData *data = UIImageJPEGRepresentation(image, 0);
for(int i=0; i<1000; i++)
{
// Convert the data with low image quality to UIImage
UIImage *image = [UIImage imageWithData:data];
// Compress the image into a low quality data again
// at this point i would imagine the image get even more low quality, like u resaved a jpeg twice in phootshop
data = UIImageJPEGRepresentation(image, 0);
}
// up to this point I would imagine the "data" has gone through JPEG compression 1000 times
// like you resave a jpeg as a jpeg in photoshop 1000 times, it should look like a piece of crap
UIImage *imageFinal = [UIImage imageWithData:data];
UIImageView *view = [[UIImageView alloc] initWithImage:imageFinal];
[self.view addSubview:view];
// but it didn't, the final image looks like it has only gone through the jpeg compression once.
EDIT: my doubt can be summarised into a simpler code, if you do this in objectiveC:
UIImage *image1 = an image..
NSData *data1 = UIImageJPEGRepresentation(image1, 0);
UIImage *image2 = [UIImage imageWithData:data1];
NSData *data2 = UIImageJPEGRepresentation(image2, 0);
UIImage *imageFinal = [UIImage imageWithData:data2];
Did imageFinal gone through JPEG compression twice?
As you know, JPG compression works by altering the image to produce smaller file size. The reason why you don't see progressively worse quality is because you're using the same compression setting each time.
The algorithm alters the source image just enough to fit into the compression profile - in other words, compressing the result of 50% JPG again at 50% will produce the same image, because the image doesn't need to be altered any more.
You can test this in Photoshop - save a photo out at say 30% quality JPG. Reopen the file you just saved, and go to Save for Web - flip between PNG (uncompressed/original) and JPG 30% - there will be no difference.
Hope this helps.
All types of compression will ideally reduce the size of an image. There are two types of compression which describes how they affects images:
Lossy Compression:
Lossy compression will reduces the size of the image by removing some data from it. This generally cause, effect the quality of the image, which means it reduce your image quality
Lossless Compression:
Lossless compression reduce the size of the image by changing the way in which the data is stored. Therefore this type of compression will make no change in the image quality.
Please check out the compression type you are using.
This may help you in decrease the image size. put the number from yourself how many times you want to perform loop;
UIImage *image = [UIImage imageNamed:#"photo.jpeg"];
for(int i=100; i>0; i--)
{
UIImage *image = [UIImage imageWithData:data];
NSData *data = UIImageJPEGRepresentation(image, (0.1 * i);
NSLog(#"%d",data.length);
}

Resources