本文整理汇总了C#中IApiClient.GetImageStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IApiClient.GetImageStreamAsync方法的具体用法?C# IApiClient.GetImageStreamAsync怎么用?C# IApiClient.GetImageStreamAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IApiClient
的用法示例。
在下文中一共展示了IApiClient.GetImageStreamAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRemoteBitmapAsyncInternal
/// <summary>
/// Gets the remote bitmap async.
/// </summary>
/// <param name="apiClient">The API client.</param>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{BitmapImage}.</returns>
/// <exception cref="ArgumentNullException">url</exception>
private async Task<BitmapImage> GetRemoteBitmapAsyncInternal(IApiClient apiClient, string url, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
var cachePath = _remoteImageCache.GetResourcePath(url.GetMD5().ToString());
try
{
return GetCachedBitmapImage(cachePath);
}
catch (IOException)
{
// Cache file doesn't exist or is currently being written to.
}
cancellationToken.ThrowIfCancellationRequested();
var semaphore = GetImageFileLock(cachePath);
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
// Look in the cache again
try
{
var img = GetCachedBitmapImage(cachePath);
semaphore.Release();
return img;
}
catch (IOException)
{
// Cache file doesn't exist or is currently being written to.
}
try
{
using (var httpStream = await apiClient.GetImageStreamAsync(url, cancellationToken).ConfigureAwait(false))
{
var parentPath = Path.GetDirectoryName(cachePath);
if (!Directory.Exists(parentPath))
{
Directory.CreateDirectory(parentPath);
}
using (var fileStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
await httpStream.CopyToAsync(fileStream).ConfigureAwait(false);
}
return GetCachedBitmapImage(cachePath);
}
}
finally
{
semaphore.Release();
}
}
示例2: UpdateUserImage
private async Task UpdateUserImage(UserDto user, IApiClient apiClient, CancellationToken cancellationToken)
{
if (user.HasPrimaryImage)
{
var isImageCached = await _localAssetManager.HasImage(user).ConfigureAwait(false);
if (!isImageCached)
{
var imageUrl = apiClient.GetUserImageUrl(user, new ImageOptions
{
ImageType = ImageType.Primary
});
using (var stream = await apiClient.GetImageStreamAsync(imageUrl, cancellationToken).ConfigureAwait(false))
{
await _localAssetManager.SaveImage(user, stream).ConfigureAwait(false);
}
}
}
else
{
await _localAssetManager.DeleteImage(user).ConfigureAwait(false);
}
}