Gustash / react-native-image-keyboard

React Native TextInput expansion to enable media input.
MIT License
146 stars 14 forks source link

[iOS]: paste after 30min being "copied" makes the app crashes #43

Open laurentlouk opened 6 months ago

laurentlouk commented 6 months ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-image-keyboard@2.2.1 for the project I'm working on.

The issue I have is when I copy an image from the photo library on iOS then wait for 30 minutes and paste it into a TextInput the app crashes (Physical device production/debug and Virtual Device).

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m b/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
index 90b89d9..f9ea98f 100644
--- a/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
+++ b/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
@@ -68,31 +68,40 @@ static NSArray *acceptedTypes;
             image = [self extractImageFromPasteboard];
         }
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-            NSString *mimeType = [image mimeTypeByGuessingFromData];
-            NSString *fileExtension = [mimeType stringByReplacingOccurrencesOfString:@"image/"
-                                                                          withString:@""];
-
-            NSString *base64 = [image base64EncodedStringWithOptions:0];
-
-            NSArray<NSString*> *paths = NSSearchPathForDirectoriesInDomains(
-                                                                            NSDocumentDirectory,
-                                                                            NSUserDomainMask,
-                                                                            YES);
-            NSUUID *uuid = [NSUUID UUID];
-            NSString *uniqueFileName = [uuid UUIDString];
-            NSString *path = [NSString stringWithFormat:@"%@/%@.%@",
-                              paths[0],
-                              uniqueFileName,
-                              fileExtension];
-
-            [image writeToFile:path atomically:YES];
-
-            NSLog(@"%@", path);
-            self.onImageChange(@{
-                @"data": base64,
-                @"uri": path,
-                @"mime": mimeType,
-                              });
+            if (image) {
+                NSString *mimeType = [image mimeTypeByGuessingFromData];
+                if (mimeType) {
+                    NSString *fileExtension = [mimeType stringByReplacingOccurrencesOfString:@"image/" withString:@""];
+                    NSString *base64 = [image base64EncodedStringWithOptions:0];
+
+                    NSArray<NSString*> *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+                    NSUUID *uuid = [NSUUID UUID];
+                    NSString *uniqueFileName = [uuid UUIDString];
+                    NSString *path = [NSString stringWithFormat:@"%@/%@.%@", paths[0], uniqueFileName, fileExtension];
+
+                    NSError *error = nil;
+                    [image writeToFile:path options:NSDataWritingAtomic error:&error];
+                    if (!error) {
+                        NSLog(@"%@", path);
+                        self.onImageChange(@{@"data": base64, @"uri": path, @"mime": mimeType});
+                    } else {
+                        NSLog(@"Error writing file: %@", error);
+
+                        // Call the normal paste action as fallback
+                        [[self backedTextInputView] paste:sender];
+                    }
+                } else {
+                    NSLog(@"Error: MIME type is nil");
+
+                    // Call the normal paste action as fallback
+                    [[self backedTextInputView] paste:sender];
+                }
+            } else {
+                NSLog(@"Error: Image data is nil");
+
+                // Call the normal paste action as fallback
+                [[self backedTextInputView] paste:sender];
+            }
         });
     } else {
         // Call the normal paste action

This issue body was partially generated by patch-package.

mowaisch commented 6 months ago

I faced this issue.