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

[Feature] Add support fileName propertie on iOS for captureRef method #552

Open
wanderbon opened this issue Dec 4, 2024 · 0 comments
Open

Comments

@wanderbon
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.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-view-shot/README.md b/node_modules/react-native-view-shot/README.md
index f75aa8e..5cb0fe8 100644
--- a/node_modules/react-native-view-shot/README.md
+++ b/node_modules/react-native-view-shot/README.md
@@ -133,7 +133,7 @@ Returns a Promise of the image URI.
 
 - **`view`** is a reference to a React Native component.
 - **`options`** may include:
-  - **`fileName`** _(string)_: (Android only) the file name of the file. Must be at least 3 characters long.
+  - **`fileName`** _(string)_: the file name of the file. Must be at least 3 characters long.
   - **`width`** / **`height`** _(number)_: the width and height of the final image (resized from the View bound. don't provide it if you want the original pixel size).
   - **`format`** _(string)_: either `png` or `jpg` or `webm` (Android). Defaults to `png`.
   - **`quality`** _(number)_: the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpg)
diff --git a/node_modules/react-native-view-shot/ios/RNViewShot.m b/node_modules/react-native-view-shot/ios/RNViewShot.m
index 9015847..f619aad 100644
--- a/node_modules/react-native-view-shot/ios/RNViewShot.m
+++ b/node_modules/react-native-view-shot/ios/RNViewShot.m
@@ -67,6 +67,7 @@ RCT_EXPORT_METHOD(captureRef:(nonnull NSNumber *)target
     CGSize size = [RCTConvert CGSize:options];
     NSString *format = [RCTConvert NSString:options[@"format"]];
     NSString *result = [RCTConvert NSString:options[@"result"]];
+    NSString *fileName = [RCTConvert NSString:options[@"fileName"]];
     BOOL renderInContext = [RCTConvert BOOL:options[@"useRenderInContext"]];
     BOOL snapshotContentContainer = [RCTConvert BOOL:options[@"snapshotContentContainer"]];
 
@@ -162,7 +163,12 @@ RCT_EXPORT_METHOD(captureRef:(nonnull NSNumber *)target
       }
       else {
         // Save to a temp file
-        NSString *path = RCTTempFilePath(format, &error);
+          NSString *path = TempFilePath(
+                                           fileName != nil
+                                            ? fileName
+                                            : [NSUUID new].UUIDString,
+                                           format,
+                                           &error);
         if (path && !error) {
           if ([data writeToFile:path options:(NSDataWritingOptions)0 error:&error]) {
             res = path;
@@ -182,5 +188,48 @@ RCT_EXPORT_METHOD(captureRef:(nonnull NSNumber *)target
   }];
 }
 
+NSString *__nullable TempFilePath(NSString *fileName, NSString *extension, NSError **error)
+{
+  static NSError *setupError = nil;
+  static NSString *directory;
+  static dispatch_once_t onceToken;
+  dispatch_once(&onceToken, ^{
+    directory = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ReactNative"];
+    // If the temporary directory already exists, we'll delete it to ensure
+    // that temp files from the previous run have all been deleted. This is not
+    // a security measure, it simply prevents the temp directory from using too
+    // much space, as the circumstances under which iOS clears it automatically
+    // are not well-defined.
+    NSFileManager *fileManager = [NSFileManager new];
+    if ([fileManager fileExistsAtPath:directory]) {
+      [fileManager removeItemAtPath:directory error:NULL];
+    }
+    if (![fileManager fileExistsAtPath:directory]) {
+      NSError *localError = nil;
+      if (![fileManager createDirectoryAtPath:directory
+                  withIntermediateDirectories:YES
+                                   attributes:nil
+                                        error:&localError]) {
+        // This is bad
+        RCTLogError(@"Failed to create temporary directory: %@", localError);
+        setupError = localError;
+        directory = nil;
+      }
+    }
+  });
+
+  if (!directory || setupError) {
+    if (error) {
+      *error = setupError;
+    }
+    return nil;
+  }
+
+  if (extension) {
+    fileName = [fileName stringByAppendingPathExtension:extension];
+  }
+  return [directory stringByAppendingPathComponent:fileName];
+}
+
 
 @end

This issue body was partially generated by patch-package.

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

1 participant