Skip to content

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

Open
@wanderbon

Description

@wanderbon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions