// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UltimateXR.Extensions.System;
using UltimateXR.Extensions.System.IO;
using UnityEngine.UI;
namespace UltimateXR.Extensions.Unity.Render
{
///
/// extensions.
///
public static class ImageExt
{
#region Public Methods
///
/// Loads a sprite asynchronously from a base64 encoded string and assigns it to the
/// property of an .
///
/// Target
/// Base64 encoded string. See
/// Optional cancellation token, to cancel the operation
/// is null or empty
/// Task canceled using
///
/// The length of , ignoring white-space characters, is not
/// zero or a multiple of 4
///
public static async Task OverrideSpriteFromBase64Async(this Image self, string base64, CancellationToken ct = default)
{
self.ThrowIfNull(nameof(self));
self.overrideSprite = await SpriteExt.ReadSpriteBase64Async(self, base64, ct);
}
///
/// Loads a sprite asynchronously from an URI and assigns it to the property of an
/// .
///
/// Target image
/// File location. See
/// Optional cancellation token, to cancel the operation
///
/// is null or empty
/// Task canceled using
/// The file specified in was not found
/// is in an invalid format
/// An I/O error occurred while opening the file
/// The stream is currently in use by a previous read operation
public static async Task OverrideSpriteFromUriAsync(this Image self, string uri, CancellationToken ct = default)
{
self.ThrowIfNull(nameof(self));
self.overrideSprite = await SpriteExt.ReadSpriteFileAsync(self, uri, ct);
}
///
/// Tries to load a sprite asynchronously from an URI and assign it to the property
/// of an .
///
/// Target image
/// File location. See
/// Optional cancellation token, to cancel the operation
///
/// Whether the sprite was correctly load and the had its
/// assigned
///
public static async Task TryOverrideSpriteFromUriAsync(this Image self, string uri, CancellationToken ct = default)
{
try
{
await self.OverrideSpriteFromUriAsync(uri, ct);
return true;
}
catch
{
return false;
}
}
///
/// Tries to load a sprite asynchronously from a base64 encoded string and assign it to the
/// property of an .
///
/// Target image
/// Base64 encoded string with the image file content
/// Optional cancellation token, to cancel the operation
///
/// Whether the sprite was correctly load and the had its
/// assigned
///
public static async Task TryOverrideSpriteFromBase64Async(this Image self, string base64, CancellationToken ct)
{
try
{
await self.OverrideSpriteFromBase64Async(base64, ct);
return true;
}
catch
{
return false;
}
}
#endregion
}
}