PhotographSaveView.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. base.Dispose();
  40. }
  41. private void OnClickBtnSave()
  42. {
  43. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  44. //判断目录是否存在,不存在则会创建目录
  45. if (!Directory.Exists(path))
  46. {
  47. try
  48. {
  49. Directory.CreateDirectory(path);
  50. }
  51. catch (Exception exception)
  52. {
  53. throw new Exception("创建文件夹失败, error:" + exception.Message);
  54. }
  55. }
  56. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  57. Texture2D tex = this.viewData as Texture2D;
  58. byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  59. var filePath = path + fileName;
  60. File.WriteAllBytes(filePath, bytes);
  61. UpdateSystemPhoto(filePath);
  62. }
  63. //调用iOS或Android原生方法保存图片后更新相册.
  64. private void UpdateSystemPhoto(string filePath)
  65. {
  66. #if UNITY_ANDROID
  67. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  68. androidJavaObject.Call("scanFile", filePath, "已保存至相册"); //这里我们可以设置保存成功弹窗内容
  69. #endif
  70. }
  71. protected override void UpdateToCheckGuide(object param)
  72. {
  73. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  74. GuideController.TryCompleteGuide(ConstGuideId.PHOTOGRAPH, 3);
  75. }
  76. }
  77. }