剛寫完自定義頭像模塊,記錄一下剛纔的過程,直接上代碼: 在講位元組轉化為string時,網上很多資料都是 string str = System.Text.Encoding.UTF8.GetString(imgByte); 但是親測返回值是亂碼,上傳的PHP時無法解析,所以嘗試使用Convert.To ...
剛寫完自定義頭像模塊,記錄一下剛纔的過程,直接上代碼:
public static string ImgByte() { //獲取圖片地址 string path = UnityEngine.Application.persistentDataPath + "headIcon/icon.png"; //將圖片轉換為流 FileStream files = new FileStream(path, FileMode.Open); byte[] imgByte = new byte[files.Length]; //將流存儲為位元組 files.Read(imgByte, 0, imgByte.Length); files.Close(); //再將位元組轉化為string string str = Convert.ToBase64String(imgByte); Debug.Log("數據的長度:" + imgByte.Length); Debug.Log("字元串的長度:" + str.Length); return str; }
在講位元組轉化為string時,網上很多資料都是
string str = System.Text.Encoding.UTF8.GetString(imgByte);
但是親測返回值是亂碼,上傳的PHP時無法解析,所以嘗試使用Convert.ToBase64String進行轉碼,成功。