Currently working on an app, which downloads images (jpg and png) from an url, once downloaded, it wont be download again.
After the download process, the files are loaded into UGUI (RawImage).
It works great in editor and android, but crashes on IOS.
Here is the the code snippets (COPIED ALL THE RELEVANT SECTIONS):
Download images:
IEnumerator loadBgImage(string _imgName)
{
string publisherDir = Application.persistentDataPath + "/" + STATIC.PUBLISER_NAME;
string bookDir = Application.persistentDataPath + "/" + STATIC.PUBLISER_NAME + "/" + STATIC.BOOK_ID;
if (!System.IO.Directory.Exists(publisherDir))
{
System.IO.Directory.CreateDirectory(publisherDir);
}
if (!System.IO.Directory.Exists(bookDir))
{
System.IO.Directory.CreateDirectory(bookDir);
}
WWW www = new WWW(STATIC.BOOK_URL + "/images/" + _imgName);
yield return www;
System.IO.File.WriteAllBytes(bookDir + "/" + _imgName, www.bytes);
}
loading into UGUI:
IEnumerator loadBgImage(UnityEngine.UI.RawImage _targtImg, string _imgName)
{
WWW www = new WWW("file://" + bookDir + "/" + _imgName);
yield return www;
byte[] imgBytes = www.bytes;
Texture2D text2d = null;
text2d = new Texture2D(16, 16,TextureFormat.PVRTC_RGB2,false);
text2d.LoadImage(imgBytes);
_targtImg.texture = text2d;
RectTransform rect = _targtImg.GetComponent();
if (rect != null)
{
Vector2 offset = Vector2.zero;
rect.offsetMin = offset;
rect.offsetMax = offset;
rect.localScale = Vector3.one;
}
}
The app crashes straight after loading all the images into the RawImage.
Is there a better way to do this?
Where am I going wrong?
Am I using the correct format for the Texture?
↧