• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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?

2 Replies

· Add your reply
  • Sort: 
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

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

Texture display incorrect on iOS / Mac Editor 0 Answers

Skybox Texture doesn't display correctly on iOS build 1 Answer

Assetbundle memory leaks 0 Answers

Texture looks low-res/chunky/pixelated on mobile 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges