- Home /
How to save textures to iOS devices, ask for help.
Now I need to implement saving images to my iOS device. I have seen many online examples using "ScreenCapture.CaptureScreenshot(filepach);" for screenshot capture, but what I need is to save the texture image prepared in Unity. Please ask me What do you need to do.
Comment
Best Answer
Answer by Grish_tad · Nov 06, 2018 at 05:07 PM
Hi @Vortexboy there is a simple way to save textures to your device.
IEnumerator SaveTexture(Texture2D texture)
{
string fileName = "Photo";
string screenshotFilename;
string path;
string date = System.DateTime.Now.ToString("ddMMyyHHmmss");
screenshotFilename = fileName + "_" + date + ".png";
if (Application.platform == RuntimePlatform.Android)
{
string androidPath = "/../../../../DCIM/YourAppName/" + screenshotFilename;
path = Application.persistentDataPath + androidPath;
string pathonly = Path.GetDirectoryName(path);
if (!Directory.Exists(pathonly))
{
Directory.CreateDirectory(pathonly);
}
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
string iosPath = Application.persistentDataPath + "/" + screenshotFilename;
path = iosPath;
}
else
{
string otherPath = screenshotFilename;
path = otherPath;
}
byte[] bytes = texture.EncodeToJPG();
File.WriteAllBytes(path, bytes);
yield return new WaitForEndOfFrame();
}
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;
public class SaveImage : $$anonymous$$onoBehaviour
{
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void _SavePhoto(string readAddr);
#endif
public Texture2D[] textures;
private Texture2D texture;
private string m_ImageFileName;
public void OnCilck()
{
StartCoroutine(SaveImages());
}
IEnumerator SaveImages()
{
texture = textures[Random.Range(0, textures.Length)];
m_ImageFileName = texture.name +".png";
string path = Application.persistentDataPath;
#if UNITY_ANDROID
path = "/sdcard/DCI$$anonymous$$/Save Image";
#elif UNITY_IOS
path = Application.persistentDataPath;
#endif
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string savePath = path + "/" + m_ImageFileName;
File.WriteAllBytes(savePath, texture.EncodeToPNG());
savePng(savePath);
yield return new WaitForEndOfFrame();
}
public void savePng(string fileName)
{
#if UNITY_ANDROID
GetAndroidJavaObject().Call("scanFile", fileName);
#elif UNITY_IOS
_SavePhoto(m_ImageFileName);
#endif
}
#if UNITY_ANDROID
private AndroidJavaObject GetAndroidJavaObject()
{
return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
}
#endif
}
Hello, I used your method, but still did not save successfully, this is my code.
iOS saved it to, but you need some libraries to show saved images in galery.
Thank you very much, it turned out that my "path" was wrong. Now I can successfully save the image and view the saved image in the iPad album.
public void savePng(string fileName)
{
#if UNITY_ANDROID
GetAndroidJavaObject().Call("scanFile", fileName);
#elif UNITY_IOS
//_SavePhoto(m_ImageFileName);
_SavePhoto(fileName);
#endif
}