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?

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!

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.

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

@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.