Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JACLEMGO · May 20, 2015 at 03:46 PM · iostexturememory-leakiounitygui

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abi-kr01 · May 21, 2015 at 06:40 AM 0
Share

it should be in .png or in jpg. raw files seems to have problem.

avatar image Nerevar · Feb 17, 2016 at 02:24 PM 0
Share

It crashes when you load all the images? or it crashes also with just one?

4 Replies

· Add your reply
  • Sort: 
avatar image
4

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkoHazel · May 28, 2020 at 05:00 AM 0
Share

Dude this is amazing! I've been looking for something like this for AGES!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by teja6595 · Mar 12, 2020 at 04:49 AM

@JACLEMGO

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges