RNViewShotModule.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Newtonsoft.Json.Linq;
  2. using ReactNative.Bridge;
  3. using ReactNative.UIManager;
  4. using System;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. namespace RNViewShot
  8. {
  9. /// <summary>
  10. /// A module that allows JS to share data.
  11. /// </summary>
  12. class RNViewShotModule : ReactContextNativeModuleBase
  13. {
  14. private const string ErrorUnableToSnapshot = "E_UNABLE_TO_SNAPSHOT";
  15. private readonly ReactContext _reactContext;
  16. /// <summary>
  17. /// Instantiates the <see cref="RNViewShotModule"/>.
  18. /// </summary>
  19. public RNViewShotModule(ReactContext reactContext) : base(reactContext)
  20. {
  21. this._reactContext = reactContext;
  22. }
  23. /// <summary>
  24. /// The name of the native module.
  25. /// </summary>
  26. public override string Name
  27. {
  28. get
  29. {
  30. return "RNViewShot";
  31. }
  32. }
  33. [ReactMethod]
  34. public void takeSnapshot(int tag, JObject options, IPromise promise)
  35. {
  36. string format = options["format"] != null ? options.Value<string>("format") : "png";
  37. double quality = options["quality"] != null ? options.Value<double>("quality") : 1.0;
  38. int? width = options["width"] != null ? options.Value<int?>("width") : null;
  39. int? height = options["height"] != null ? options.Value<int?>("height") : null;
  40. string result = options["result"] != null ? options.Value<string>("result") : "file";
  41. string path = options["path"] != null ? options.Value<string>("path") : null;
  42. if (format != "png" && format != "jpg" && format != "jpeg")
  43. {
  44. promise.Reject(ViewShot.ErrorUnableToSnapshot, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
  45. return;
  46. }
  47. UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
  48. var viewShot = new ViewShot(tag, format, quality, width, height, path, result, promise);
  49. uiManager.AddUIBlock(viewShot);
  50. }
  51. }
  52. }