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

Crashes the app, as IOS has deprecated UIGraphicsBeginImageContextWithOptions from IOS 17. #1010

Open
dprajapati1179 opened this issue Oct 5, 2023 · 7 comments

Comments

@dprajapati1179
Copy link

dprajapati1179 commented Oct 5, 2023

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Getting crash on iOS 17 as UIGraphicsBeginImageContextWithOptions.
It crashes the app, as IOS has deprecated UIGraphicsBeginImageContextWithOptions from IOS 17.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m b/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
index f710081..fe20770 100644
--- a/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
+++ b/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
@@ -73,12 +73,17 @@ - (void) setImageColor: (UIColor*)imageColor {
 
 - (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
     UIImage* newImage = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
-    UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
-    [color set];
-    [newImage drawInRect: CGRectMake(0, 0, image.size.width, newImage.size.height)];
-    newImage = UIGraphicsGetImageFromCurrentImageContext();
-    UIGraphicsEndImageContext();
-    return newImage;
+    
+    UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
+    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:image.size format:format];
+
+    UIImage *resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
+        CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
+        [color set];
+        [newImage drawInRect:rect];
+    }];
+
+    return resultImage;
 }
 
 - (void) setImage: (UIImage*)image {
@abakers
Copy link

abakers commented Oct 6, 2023

Had the same issue, your solution solved it for us. Thank you!

sterlingwes added a commit to MasterHealth-Technologies/react-native-fast-image that referenced this issue Oct 8, 2023
@tkyr-hh
Copy link

tkyr-hh commented Oct 9, 2023

@dprajapati1179 I really appreciate sharing your solution, can you please open a PR with this Fix??

@mzbyszynski
Copy link

FYI Pr is #1012

Note that the change in pr #1007 is also very similar and should fix this isssue.

@dtyrrell
Copy link

Do you have repro steps for this @dprajapati1179 ?

I'm running 8.6.3 and my app seems fine on iOS 17 & 17.1 based on the limited testing i've done.

@Pratik-27
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Getting crash on iOS 17 as UIGraphicsBeginImageContextWithOptions. It crashes the app, as IOS has deprecated UIGraphicsBeginImageContextWithOptions from IOS 17.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m b/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
index f710081..fe20770 100644
--- a/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
+++ b/node_modules/react-native-fast-image/ios/FastImage/FFFastImageView.m
@@ -73,12 +73,17 @@ - (void) setImageColor: (UIColor*)imageColor {
 
 - (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
     UIImage* newImage = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
-    UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
-    [color set];
-    [newImage drawInRect: CGRectMake(0, 0, image.size.width, newImage.size.height)];
-    newImage = UIGraphicsGetImageFromCurrentImageContext();
-    UIGraphicsEndImageContext();
-    return newImage;
+    
+    UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
+    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:image.size format:format];
+
+    UIImage *resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
+        CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
+        [color set];
+        [newImage drawInRect:rect];
+    }];
+
+    return resultImage;
 }
 
 - (void) setImage: (UIImage*)image {

Thanks for this solution but i found that tintColor prop does not work in this solution hence providing an alternative for the same:

  • (UIImage*)makeImage:(UIImage *)image withTint:(UIColor *)color {
    UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:image.size format:format];

    UIImage *resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    [newImage drawInRect:rect];
    CGContextRef context = rendererContext.CGContext;
    CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
    [color setFill];
    CGContextFillRect(context, rect);
    }];

    return resultImage;
    }

@nes123
Copy link

nes123 commented Jun 13, 2024

After implementing this patch there is a new error, "nan" in size rather then "0":

UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={nan, nan}, scale=1.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer

any ideas what can cause that?

@codeGV
Copy link

codeGV commented Jul 25, 2024

If you don't want changes in node_modules. Use this package (froked) - https://www.npmjs.com/package/@codegv/react-native-fast-image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants