PhotographSaveView.cs 2.5 KB

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