ios ios - 2 months ago 6
Objective-C Question

the image is not compressed in ios

the image is not compressed Im using the following code

UIImage *image = [UIImage imageNamed:@"1.png"];
CGSize newSize = CGSizeMake(100.0f, 100.0f);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_backgroundImageView.image = newImage;

Answer

If you want to compress the image

UIImage *img = [UIImage imageNamed:@"yourimagename"];
NSData *postData = UIImageJPEGRepresentation(img, 1.0);
NSInteger imgSizeBytes = [postData length];
double imgSizeKBytes = ceil((double)imgSizeBytes / 1024);
NSString *strBytes;
if(imgSizeKBytes > 1024) {
    double imgSizeMBytes = (double)imgSizeKBytes / 1024;
    strBytes = [NSString stringWithFormat:@"%.1f MB", imgSizeMBytes];
}
else {
    strBytes = [NSString stringWithFormat:@"%d KB", (int)imgSizeKBytes];
}

imageView.image = [UIImage imageWithData:postData scale:0.1];

You can compress with your required image size by using below code

NSData *postData = UIImageJPEGRepresentation(img, (double)(100-75)/100); 

Above I have given 75.If you want to give your size,give there.