- Home /
Download images from url, save to device, load into UGUI
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<RectTransform>();
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?
it should be in .png or in jpg. raw files seems to have problem.
It crashes when you load all the images? or it crashes also with just one?
Answer by mamad_m2 · Mar 11, 2020 at 10:46 PM
For everyone else, to download and display images in Unity you can easily use Davinci. This library has a simple usage and supports Unity UI.Image and 3D model textures and lots of other cool features like download progress, placeholders and etc.
Davinci.get().load(imageUrl).into(image).start();
Hope this helps!
Dude this is amazing! I've been looking for something like this for AGES!
Answer by Nilesh Kumar · Feb 17, 2016 at 04:52 PM
www class already convert in texture no need to convert manually:
IEnumerator loadBgImage (UnityEngine.UI.RawImage _targtImg, string _imgName) {
WWW www = new WWW ("file://" + bookDir + "/" + _imgName);
yield return www;
_targtImg.texture = www.texture;
RectTransform rect = _targtImg.GetComponent<RectTransform> ();
if (rect != null) {
Vector2 offset = Vector2.zero;
rect.offsetMin = offset;
rect.offsetMax = offset;
rect.localScale = Vector3.one;
}
}
Try this.it will work.
Answer by zulfiqar-younus · Mar 01, 2017 at 07:16 AM
refined code for same problem.
IEnumerator loadBgImage(Image _targtImg, string imgURL) { WWW www = new WWW(imgURL); yield return www; _targtImg.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f)); }
Answer by teja6595 · Mar 12, 2020 at 04:49 AM
Actually the main problem lies in the below line of code
text2d = new Texture2D(16, 16,TextureFormat.PVRTC_RGB2,false);
You are trying to convert the texture format to PVRTC_RGB2. This might be the reason for crash in IOS. Trying using default texture format.
text2d = new Texture2D(16, 16);
Not all Graphics supports all texture format. use SystemInfo.SupportsTextureFormat to check the supported formats.
Hope this helps.
Your answer
Follow this Question
Related Questions
Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers
Images not importing when platform is ios? 0 Answers
My ios Project is too big!?what can i do? 1 Answer
Texture on instantiated asset bundle sometimes black only on iPhone 0 Answers
Possible Texture memory leak 1 Answer