Reputation: 153
UIImage *image = [UIImage imageNamed:@"anyImage.png"];
NSData *data = UIImageJPEGRepresentation(image, 0.032);
UIImage *newImage = [UIImage imageWithData:data];
I found this size reduction of image but my problem is that i need to store only 100kb of images. In my library i have 8159064kb of image which reduce upto 141258kb by this method. Please tell me a way to reduce size of image upto 100kb whatever size come it convert it to 100kb. Thanks in advance
Upvotes: 0
Views: 2032
Reputation: 4731
you should resize the image.size
so it can reduce your large image to 100k
You can make a UIImage
category
like UIImage(Resize)
+(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;
}
And then make a while
loop to resize the image size to fit for <= 100KB
NSData *data = UIImagePNGRepresentation(_yourImage);
while (data.length / 1000 >= 100) {
_yourImage = [UIImage imageWithImage:_yourImage andWidth:image.size.width/2 andHeight:image.size.height/2];
data = UIImagePNGRepresentation(_yourImage);
}
// _yourImage is now reduce the size which is <= 100KB
Upvotes: 2