• [游戏开发-学习笔记]菜鸟慢慢飞(15)- Unity3D-图片压缩


    先看代码:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using System.Drawing;
    using System;
    
    /// <summary>
    /// 图片压缩工具
    /// </summary>
    public class CompressPictureHelper :MonoBehaviour
    {
    
        private static CompressPictureHelper _instance;
    
        public static CompressPictureHelper Instance
        {
            get {
                if (_instance == null) {  // 如果没有找到
                    GameObject go = new GameObject ("_CompressPictureHelper"); // 创建一个新的GameObject
                    DontDestroyOnLoad (go);  // 防止被销毁
                    _instance = go.AddComponent<CompressPictureHelper> (); // 将实例挂载到GameObject上
                }
                return _instance;
            }
        }
    
        /// <summary>
        /// 图片的限制大小
        /// </summary>
          int limitI=300;
    
    /// <summary>
    /// Compresses the picture.
    /// </summary>
    /// <param name="imagePath">Image path.</param>
    /// <param name="action">成功返回原地址,失败返回null.</param>
        public  void CompressPicture(string imagePath,Action<string> action)
        {
            if (File.Exists(imagePath))
            {
                FileInfo f new FileInfo (imagePath);
    
                if ((f.Length/1024)>=limitI)
                {
    //                Debug.Log ("开始压缩,图片原始大小为:" + f.Length/1000+"Kb");
                
                    StartCoroutine (Compress(imagePath,delegate(string str)
                    {
                        action(str);
                    }));
                }
            }
        }
    
        IEnumerator Compress(string imagePath,Action<string>action)
        {  
            int qualityI = 100;
            WWW www new WWW ("file:///"+imagePath);  
            yield return www;  
            if (www.error!=null)
            {
                action (null);
                //发返回失败
            }
            else
            {
                Texture2D t2d = www.texture;
                byte[] b =  t2d.EncodeToJPG (qualityI);
                //        Debug.Log( "图原始读取的字节数 " + (b.Length/1000).ToString());
    
                while((b.Length/1024)>=limitI)
                {
                    qualityI -= 5;
                    b = t2d.EncodeToJPG (qualityI);
                    //            Debug.Log ("当前大小:"+b.Length/1000);
                }
    
                //        Debug.Log ("压缩成功,当前大小:"+b.Length/1000);
                File.WriteAllBytes (imagePath,b);
                action (imagePath);
            }
        }  
    }

    注意点:

    1 .  myTexture2D.EncodeToJPG();

      这里括号中如果不写,会默认为75.

    2 .  压缩后的图片会替代原路径下的原文件

  • 相关阅读:
    xen虚拟机管理命令
    ipmi
    http://classworlds.codehaus.org/apiusage.html
    maven编译问题之 -The POM for XXX is invalid, transitive dependencies (if any) will not be available
    SSM项目web.xml等配置文件中如何查找类的全路径名?
    shiro安全框架学习-1
    ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
    ht-7 treeSet特性
    ht-6 hashSet特性
    ht-5 treemap特性
  • 原文地址:https://www.cnblogs.com/workhai/p/6689001.html
Copyright © 2020-2023  润新知