ios ios - 1 year ago 107
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);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
_backgroundImageView.image = newImage;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download