PhotographSaveView.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using ET;
  5. using FairyGUI;
  6. using UI.DressUp;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. public class PhotographSaveView : BaseView
  11. {
  12. private UI_PhotographSaveUI _ui;
  13. protected override void OnInit()
  14. {
  15. base.OnInit();
  16. packageName = UI_PhotographUI.PACKAGE_NAME;
  17. _ui = UI_PhotographSaveUI.Create();
  18. viewCom = _ui.target;
  19. isfullScreen = true;
  20. _ui.m_btnClose.onClick.Add(this.Hide);
  21. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  22. }
  23. protected override void OnShown()
  24. {
  25. base.OnShown();
  26. Texture2D tex = this.viewData as Texture2D;
  27. _ui.m_imgRes.texture = new NTexture(tex);
  28. float width = _ui.m_imgBorder.width;
  29. float height = width * tex.height / tex.width;
  30. _ui.m_imgRes.SetSize(width, height);
  31. _ui.m_imgBorder.SetSize(width, height + 12);
  32. }
  33. protected override void OnHide()
  34. {
  35. base.OnHide();
  36. }
  37. public override void Dispose()
  38. {
  39. if (_ui != null)
  40. {
  41. _ui.Dispose();
  42. _ui = null;
  43. }
  44. base.Dispose();
  45. }
  46. private void OnClickBtnSave()
  47. {
  48. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  49. //判断目录是否存在,不存在则会创建目录
  50. if (!Directory.Exists(path))
  51. {
  52. try
  53. {
  54. Directory.CreateDirectory(path);
  55. }
  56. catch (Exception exception)
  57. {
  58. throw new Exception("创建文件夹失败, error:" + exception.Message);
  59. }
  60. }
  61. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  62. Texture2D tex = this.viewData as Texture2D;
  63. byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  64. var filePath = path + fileName;
  65. File.WriteAllBytes(filePath, bytes);
  66. UpdateSystemPhoto(filePath);
  67. }
  68. //调用iOS或Android原生方法保存图片后更新相册.
  69. private void UpdateSystemPhoto(string filePath)
  70. {
  71. #if UNITY_ANDROID
  72. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  73. androidJavaObject.Call("scanFile", filePath, "已保存至相册"); //这里我们可以设置保存成功弹窗内容
  74. #endif
  75. }
  76. // protected override void UpdateToCheckGuide(object param)
  77. // {
  78. // if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  79. // GuideController.TryCompleteGuide(ConstGuideId.PHOTOGRAPH, 3);
  80. // }
  81. }
  82. }