From 89b8791237d0c8c37af2ebd21686ac9eca87fbc1 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 22 Apr 2025 09:10:22 +0000 Subject: [PATCH] Fix iOS 17 compatibility --- .../ios/Classes/UIImage+scale.m | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/flutter_image_compress_common/ios/Classes/UIImage+scale.m b/packages/flutter_image_compress_common/ios/Classes/UIImage+scale.m index d5fa5ce..10b40fc 100644 --- a/packages/flutter_image_compress_common/ios/Classes/UIImage+scale.m +++ b/packages/flutter_image_compress_common/ios/Classes/UIImage+scale.m @@ -24,10 +24,10 @@ -(UIImage *)scaleWithMinWidth: (CGFloat)minWidth minHeight:(CGFloat)minHeight { actualHeight = floor(scaleRatio * actualHeight); CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); - UIGraphicsBeginImageContext(rect.size); - [self drawInRect:rect]; - UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:rect.size]; + UIImage *newImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { + [self drawInRect:rect]; + }]; if([ImageCompressPlugin showLog]){ NSLog(@"scale = %.2f", scaleRatio); @@ -52,22 +52,22 @@ - (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{ CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; - // Create the bitmap context - UIGraphicsBeginImageContext(rotatedSize); - CGContextRef bitmap = UIGraphicsGetCurrentContext(); - - // Move the origin to the middle of the image so we will rotate and scale around the center. - CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); - - // // Rotate the image context - CGContextRotateCTM(bitmap, (degrees * M_PI / 180)); - - // Now, draw the rotated/scaled image into the context - CGContextScaleCTM(bitmap, 1.0, -1.0); - CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]); + + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:rotatedSize]; + UIImage *newImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { + CGContextRef bitmap = context.CGContext; + + // Move the origin to the middle of the image so we will rotate and scale around the center. + CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); + + // Rotate the image context + CGContextRotateCTM(bitmap, (degrees * M_PI / 180)); + + // Now, draw the rotated/scaled image into the context + CGContextScaleCTM(bitmap, 1.0, -1.0); + CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]); + }]; - UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); return newImage; }