Shrink image without affecting the quality in objective c
Asked Answered
B

2

7

How to shrink the image without affecting the quality programmatically.

After capture the image I want to reduce the size of that image without changing the quality in objective-c.

Barquentine answered 19/10, 2016 at 10:46 Comment(4)
What size are you talking about, the size on screen or the size on disk?Crystlecs
Size of that image, if i can capture image size of 3 MB then i need to reduce the size as 500 KB before uploading on server. And it take from any device such as iphone(5,5S, 6) or ipad.Barquentine
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality) you can find many material if you google!!!Prig
Possible duplicate of reduce camera/photo library image file size for less than 100 KB in iphonePrig
C
11

Here is the code I have used compress the image

Code :

-(UIImage *)compressImage:(UIImage *)image{

    NSData *imgData = UIImageJPEGRepresentation(image, 1); //1 it represents the quality of the image.
    NSLog(@"Size of Image(bytes):%ld",(unsigned long)[imgData length]);

    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = 600.0;
    float maxWidth = 800.0;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;
    float compressionQuality = 0.5;//50 percent compression

    if (actualHeight > maxHeight || actualWidth > maxWidth){
        if(imgRatio < maxRatio){
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio){
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else{
            actualHeight = maxHeight;
            actualWidth = maxWidth;
        }
    }

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    UIGraphicsEndImageContext();

    NSLog(@"Size of Image(bytes):%ld",(unsigned long)[imageData length]);

    return [UIImage imageWithData:imageData];
}

So Here is the code to use the above

UIImage *org = [UIImage imageNamed:@"MacLehose Stage 7 Stunning Natural Sceneries.jpg"];

UIImage *imgCompressed = [self compressImage:org];

If you want to compress more

NSData *dataImage = UIImageJPEGRepresentation(imgCompressed, 0.0);

NSLog(@"Size of Image(bytes):%ld",(unsigned long)[dataImage length]);

By the above mentioned way I am able to compress the image from 2 MB to near about 50 KB.

Contemplative answered 19/10, 2016 at 11:5 Comment(6)
how to posible janamejaya sir image center part slim like curve shape for objective cUnappealable
center of image part slim shape for objective cUnappealable
@Contemplative image quality is affectingTouchhole
@YogendraPatel If you want to keep same quality, you can crop the image to required dimension, Here we are scaling and resizing so won't get quality of original imageContemplative
@Contemplative Select image that have a size of more than 5 mb then compress it and check the resultTouchhole
@YogendraPatel, As I told earlier image quality cant be like original as we are scaling, Anyway if you know better approach so that image can be compressed without affecting original quality, please post your answer here. That would be helpful to others.Contemplative
P
6

I search a lot with this topic a lot. After few days, i found this one. Hope this is much faster then the accepted answer.

NSData *imageData;
imageData=[[NSData alloc] initWithData:UIImageJPEGRepresentation((chosenImage), 1.0)];

NSLog(@"[before] image size: %lu--", (unsigned long)[imageData length]);

CGFloat scale= (100*1024)/(CGFloat)[imageData length]; // For 100KB. 


UIImage *small_image=[UIImage imageWithCGImage:chosenImage.CGImage scale:scale orientation:chosenImage.imageOrientation];

imageData = UIImageJPEGRepresentation(small_image, scale*1.00);


NSLog(@"[after] image size: %lu:%f", (unsigned long)[imageData length],scale);

It worked for me great!. Try it once.

Prosimian answered 19/10, 2016 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.