/// <summary> /// Prepares the instance before it runs. /// </summary> public override async Task<bool> PrepareAndTryLoadingFromCacheAsync() { ImageView imageView; _imageWeakReference.TryGetTarget(out imageView); if (imageView == null) return false; // Cancel current task attached to the same image view, if needed only var currentAssignedTask = imageView.GetImageLoaderTask(); if (currentAssignedTask != null) { Logger.Debug("Cancel current task attached to the same image view"); currentAssignedTask.CancelIfNeeded(); } var cacheResult = await TryLoadingFromCacheAsync(imageView).ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave return true; // stop processing if loaded from cache OR if loading from cached raised an exception bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, imageView, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { imageView.SetImageDrawable(drawable); }).ConfigureAwait(false); } return false; }
public void DrawPageImageView(ImageView holder, int position) { if (CancelPotentialWork(holder, position)) { BitmapWorkerTask task = new BitmapWorkerTask(holder, position, this); AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.Resources, mLoadingBitmap, task); holder.SetImageDrawable(asyncDrawable); task.Execute(); } }
public void LoadBitmap(string imagePath) { if (!CancelPotentialWork(imagePath, imageView)) { return; } var task = CreateBitmapWorkerTask(); var asyncDrawable = new AsyncDrawable(Resources, Placeholder, task); imageView.SetImageDrawable(asyncDrawable); task.Execute(imagePath); }
private static BitmapWorkerTask GetBitmapWorkerTask(ImageView imageView) { if (imageView != null && imageView.Drawable != null) { Drawable drawable = imageView.Drawable; if (drawable.GetType() == typeof(AsyncDrawable)) { AsyncDrawable asyncDrawable = (AsyncDrawable)drawable; return(asyncDrawable.GetBitmapWorkerTask()); } } return(null); }
public void LoadBitmap(string image, ImageView imageView) { if (!CancelPotentialWork(image, imageView)) { return; } var task = CreateBitmapWorkerTask(imageView); var asyncDrawable = new AsyncDrawable(command.Resources, command.PlaceHolder, task); imageView.SetImageDrawable(asyncDrawable); task.Execute(image); }
/// <summary> /// Loads given placeHolder into the imageView. /// </summary> /// <returns>An awaitable task.</returns> /// <param name="placeholderPath">Full path to the placeholder.</param> /// <param name="source">Source for the path: local, web, assets</param> protected async Task<bool> LoadPlaceHolderAsync(string placeholderPath, ImageSource source, ImageView imageView, bool isLoadingPlaceholder) { if (string.IsNullOrWhiteSpace(placeholderPath)) return false; if (imageView == null) return false; BitmapDrawable drawable = ImageCache.Instance.Get(GetKey(placeholderPath)); if (drawable != null) { // We should wrap drawable in an AsyncDrawable, nothing is deferred drawable = new AsyncDrawable(Context.Resources, drawable.Bitmap, this); } else { // Here we asynchronously load our placeholder: it is deferred so we need a temporary AsyncDrawable drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { imageView.SetImageDrawable(drawable); // temporary assign this AsyncDrawable }).ConfigureAwait(false); try { drawable = await RetrieveDrawableAsync(placeholderPath, source, isLoadingPlaceholder).ConfigureAwait(false); } catch (Exception ex) { Logger.Error("An error occured while retrieving drawable.", ex); return false; } } if (drawable == null) return false; _loadingPlaceholderWeakReference = new WeakReference<Drawable>(drawable); if (CancellationToken.IsCancellationRequested) return false; await MainThreadDispatcher.PostAsync(() => { if (CancellationToken.IsCancellationRequested) return; SetImageDrawable(imageView, drawable, false); }).ConfigureAwait(false); return true; }
/// <summary> /// Prepares the instance before it runs. /// </summary> public override async Task<bool> PrepareAndTryLoadingFromCacheAsync() { ImageView imageView; _imageWeakReference.TryGetTarget(out imageView); if (imageView == null) return false; if (CanUseMemoryCache()) { var cacheResult = await TryLoadingFromCacheAsync(imageView).ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave return true; // stop processing if loaded from cache OR if loading from cached raised an exception if (IsCancelled) return true; // stop processing if cancelled } bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, imageView, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { if (imageView.Handle == IntPtr.Zero) return; imageView.SetImageDrawable(drawable); }).ConfigureAwait(false); } return false; }