RNViewShotModule.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 releaseCapture (string uri) {
  35. // TODO implement me
  36. }
  37. [ReactMethod]
  38. public void captureRef(int tag, JObject options, IPromise promise)
  39. {
  40. string format = options["format"] != null ? options.Value<string>("format") : "png";
  41. double quality = options["quality"] != null ? options.Value<double>("quality") : 1.0;
  42. int? width = options["width"] != null ? options.Value<int?>("width") : null;
  43. int? height = options["height"] != null ? options.Value<int?>("height") : null;
  44. string result = options["result"] != null ? options.Value<string>("result") : "file";
  45. string path = options["path"] != null ? options.Value<string>("path") : null;
  46. if (format != "png" && format != "jpg" && format != "jpeg")
  47. {
  48. promise.Reject(ViewShot.ErrorUnableToSnapshot, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
  49. return;
  50. }
  51. UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
  52. var viewShot = new ViewShot(tag, format, quality, width, height, path, result, promise);
  53. uiManager.AddUIBlock(viewShot);
  54. }
  55. }
  56. }