Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions ios/src/Compression.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,31 @@ - (ImageResult*) compressImageDimensions:(UIImage*)image
CGFloat oldWidth = image.size.width;
CGFloat oldHeight = image.size.height;

int newWidth = 0;
int newHeight = 0;
int width = oldWidth;
int height = oldHeight;

if (maxWidth < maxHeight) {
newWidth = maxWidth;
newHeight = (oldHeight / oldWidth) * newWidth;
} else {
newHeight = maxHeight;
newWidth = (oldWidth / oldHeight) * newHeight;
if (width > maxWidth) {
height = height * (maxWidth / width);
width = maxWidth;
}

if (height > maxHeight) {
width = width * (maxHeight / height);
height = maxHeight;
}
CGSize newSize = CGSizeMake(newWidth, newHeight);

UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:newSize];
CGSize newSize = CGSizeMake(width, height);

UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = image.scale;

UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:newSize format:format];
UIImage *resizedImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
}];

result.width = [NSNumber numberWithFloat:newWidth];
result.height = [NSNumber numberWithFloat:newHeight];
result.width = [NSNumber numberWithFloat:width];
result.height = [NSNumber numberWithFloat:height];
result.image = resizedImage;
return result;
}
Expand Down