IEnumerator SaveToPhoto(string imgPath) { yield return(new WaitForEndOfFrame()); Texture2D screenImage = new Texture2D(Screen.width, Screen.height); //Get Image from screen screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); screenImage.Apply(); //Convert to png byte[] imageBytes = screenImage.EncodeToPNG(); //Save image to file File.WriteAllBytes(imgPath, imageBytes); //给个截屏效果 Color tempC = img.color; tempC.a = 0; img.color = tempC; yield return(new WaitForSeconds(0.15f)); tempC.a = 1; img.color = tempC; IOSAlbumCamera.iosSaveImageToPhotosAlbum(imgPath); }
void onclick_saveToAlbum() { string path = Application.persistentDataPath + "/lzhscreenshot.png"; Debug.Log(path); byte[] bytes = (rawImage.texture as Texture2D).EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); IOSAlbumCamera.iosSaveImageToPhotosAlbum(path); }
IEnumerator CaptureScreenshot() { //只在每一帧渲染完成后才读取屏幕信息 yield return(new WaitForEndOfFrame()); //读取屏幕像素信息并存储为纹理数据 screenShot.ReadPixels(new Rect(Screen.width * 0.14f, Screen.height * 0.37f, Screen.width * 0.69f, Screen.height * 0.48f), 0, 0); screenShot.Apply();// 这一句必须有,像素信息并没有保存在2D纹理贴图中 //读取将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); if (Application.platform == RuntimePlatform.IPhonePlayer) { string path = Application.persistentDataPath + "/QR" + Time.time.ToString() + ".png"; Debug.Log(path); File.WriteAllBytes(path, bytes); IOSAlbumCamera.iosSaveImageToPhotosAlbum(path); } //安卓平台 else if (Application.platform == RuntimePlatform.Android) { string destination = "/sdcard/DCIM"; string fileName = "erweima.png"; //判断目录是否存在,不存在则会创建目录 if (!Directory.Exists(destination)) { Directory.CreateDirectory(destination); Debug.Log("创建目录成功"); Debug.Log("destination"); } string path = destination + "/" + fileName; //存图片 System.IO.File.WriteAllBytes(path, bytes); } else { System.IO.File.WriteAllBytes(Application.persistentDataPath + "/1.png", bytes); Debug.Log(Application.persistentDataPath); } }