Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix image compression on iOS and make it respect to image scale factor #2081

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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