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?