Last active
June 10, 2023 16:42
-
-
Save akshay1188/4749253 to your computer and use it in GitHub Desktop.
Whatsapp like image compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage *)compressImage:(UIImage *)image{ | |
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(); | |
return [UIImage imageWithData:imageData]; | |
} |
Thanks, so I did that and it currently is 1280x960. But I disagree with compressionQuality = 1
in the else statement as that's the scenario where the ratios are the same, but the original image is obviously bigger, not smaller, so I do want it compressed.
Resizing with UIGraphicsImageRenderer
is dead simple but consumes a lot of memory. So i suggest to use CGContext
instead:
extension UIImage {
func compressedData() -> Data? {
let maxHeight: CGFloat = 1136
let maxWidth: CGFloat = 640
var actualHeight = size.height
var actualWidth = size.width
var imgRatio = actualWidth / actualHeight
let maxRatio = maxWidth / maxHeight
var compressionQuality : CGFloat = 0.6
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
compressionQuality = 1
}
}
guard let cgImage = cgImage else { return nil }
let mutableData = NSMutableData()
guard let imageDestinationRef = CGImageDestinationCreateWithData(mutableData as CFMutableData, kUTTypeJPEG, 1, nil) else { return nil }
let options: NSDictionary = [
kCGImageDestinationLossyCompressionQuality: compressionQuality
]
CGImageDestinationAddImage(imageDestinationRef, cgImage, options)
guard CGImageDestinationFinalize(imageDestinationRef) else { return nil }
return mutableData as Data
}
}
@knox kUTTypeJPEG deprecated
@YogeshPateliOS We can use UTType. jpeg.identifier
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Diego,
maybe, a bit difficult to check (the initial version has been created 5 years ago).. in the current WhatsApp version I see 768*1024. here how you can see
maxHeight
andmaxWidth