/// <summary>
        /// Submits a request for prefetching to the disk cache.
        /// </summary>
        /// <param name="uri">The image uri.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns>
        /// An IDataSource that can safely be ignored.
        /// </returns>
        public Task PrefetchToDiskCacheAsync(
            Uri uri,
            CancellationToken token = default(CancellationToken))
        {
            var taskCompletionSource = new TaskCompletionSource <object>();
            var dataSource           = PrefetchToDiskCache(ImageRequest.FromUri(uri), null);
            var dataSubscriber       = new BaseDataSubscriberImpl <object>(
                response =>
            {
                taskCompletionSource.SetResult(null);
                return(Task.CompletedTask);
            },
                response =>
            {
                Exception error = response.GetFailureCause();
                taskCompletionSource.SetException(error);
            });

            dataSource.Subscribe(dataSubscriber, _handleResultExecutor);
            token.Register(() =>
            {
                dataSource.Close();
                taskCompletionSource.TrySetCanceled();
            });

            return(taskCompletionSource.Task);
        }
示例#2
0
        /// <summary>
        /// Set the source URI of the image.
        /// </summary>
        /// <param name="view">The image view instance.</param>
        /// <param name="source">The source URI.</param>
        private async void SetUriFromSingleSource(Border view, string source)
        {
            var imageBrush = (ImageBrush)view.Background;

            OnImageStatusUpdate(view, ImageLoadStatus.OnLoadStart, default(ImageMetadata));
            try
            {
                var imagePipeline = ImagePipelineFactory.Instance.GetImagePipeline();
                var image         = default(BitmapSource);
                var uri           = new Uri(source);

                // Remote images
                if (source.StartsWith("http:") || source.StartsWith("https:"))
                {
                    image = await imagePipeline.FetchEncodedBitmapImageAsync(uri);
                }
                else // Base64 or local images
                {
                    image = await imagePipeline.FetchDecodedBitmapImageAsync(ImageRequest.FromUri(uri));
                }

                var metadata = new ImageMetadata(source, image.PixelWidth, image.PixelHeight);
                OnImageStatusUpdate(view, ImageLoadStatus.OnLoad, metadata);
                imageBrush.ImageSource = image;
                OnImageStatusUpdate(view, ImageLoadStatus.OnLoadEnd, metadata);
            }
            catch
            {
                OnImageFailed(view);
            }
        }
示例#3
0
        public async Task TestFetchBase64Image()
        {
            var data  = "data:image/jpeg;base64,/9j/4AAQSkZJR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LpuqWnLI3zJMhztljfo8bYOGHoQQGBAxs1+t/xp+AXhf9oDwt/ZPibTVvFiLNa3KHy7qxc4y8Ug5U8DI5VsYYMOK+HPjl/wTP8AG/w3uJrvwuV8ZaOpJVYgsOoQrz9+InbJgYGYySxyfLWvwviDgHGYKTqYNOpT8viXquvqvmkf1xwX4x5XmlOOHzOSoV+t3aEvNSfw+kn5Js+c6Kk1WwudA1SSw1C1utO1CH/W2t3C1vPH/vRuAy9D1Hao818FKLi+WSsz9jp1Izipwd09mtUFFGaZNcR265kdUH+0cVJY/NGclR1ZmCqByWJOAAO5J4A7k16j8Hf2L/iL8bZ4307QJ9L0yQgtqWrhrO3A65QMPMkyOhjRlyOWXrX25+zJ+wZ4U/Z5mh1Sdm8SeKo1ONSuoQkdoSMH7PDkiPjjcSz/ADMNwU7a+uyPgvMMxkpOPs6f80lb7lu/y8z814t8U8lySEoRmq1bpCDT1/vSV1Hz3l2izzL9g/8AYNm8JXtl468dWfl6pHifSNImX5rA/wANxOv/AD2HVIz/AKv7zfvMCL7Eoor96yfJ8NluGWGwy06vq33fmfx7xNxNjs9x0sdj5Xk9EltFdIxXRL729W22FFFFeofPhRRRQAUUUUAYvirwJofxB037Hr2jaTrloM4g1C0juYxkDPyuCK/Of9qrwTovhfxLfR6bpGl6dHHKyqlrapCqjPQBQKKK/I/ET4kf0j4J/A/X/M83+E2lWuqeIYY7q2t7iMuAVljDqeR2NfpJ+zR8IfCfhvwFperad4X8O2GqSR5e8ttNhiuHPI5dVDHjjrRRXg8B/wC+L+ux9b4wf8i+f9dGeqKadRRX70fyCFFFFABRRRQB/9k=";
            var uri   = new Uri(data);
            var image = await _imagePipeline.FetchDecodedBitmapImageAsync(
                ImageRequest.FromUri(uri)).ConfigureAwait(false);

            Assert.IsTrue(image.GetType() == typeof(WriteableBitmap));
            Assert.IsTrue(_imagePipeline.IsInBitmapMemoryCache(uri));
        }
示例#4
0
        public async Task TestFetchLocalJpeg()
        {
            var bitmap = await _imagePipeline.FetchDecodedBitmapImageAsync(
                ImageRequest.FromUri(LOCAL_JPEG_URL)).ConfigureAwait(false);

            await DispatcherHelpers.RunOnDispatcherAsync(() =>
            {
                Assert.IsTrue(bitmap.PixelWidth != 0);
                Assert.IsTrue(bitmap.PixelHeight != 0);
            });
        }
        private async void FetchDecodedImage()
        {
            try
            {
                Uri             uri    = MainPage.GenerateImageUri();
                WriteableBitmap bitmap = await _imagePipeline.FetchDecodedBitmapImageAsync(
                    ImageRequest.FromUri(uri));

                UpdateImageGrid(bitmap);
            }
            catch (Exception)
            {
                // Invalid uri, try again
                FetchDecodedImage();
            }
        }
示例#6
0
        public void TestFetchDecodedImageSuccess()
        {
            var completion     = new ManualResetEvent(false);
            var dataSource     = _imagePipeline.FetchDecodedImage(ImageRequest.FromUri(IMAGE_URL), null);
            var dataSubscriber = new BaseDataSubscriberImpl <CloseableReference <CloseableImage> >(
                async response =>
            {
                CloseableReference <CloseableImage> reference = response.GetResult();
                if (reference != null)
                {
                    SoftwareBitmap bitmap = ((CloseableBitmap)reference.Get()).UnderlyingBitmap;

                    try
                    {
                        Assert.IsTrue(bitmap.PixelWidth != 0);
                        Assert.IsTrue(bitmap.PixelHeight != 0);
                        Assert.IsTrue(_imagePipeline.IsInBitmapMemoryCache(ImageRequest.FromUri(IMAGE_URL)));
                        Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
                    }
                    catch (Exception)
                    {
                        Assert.Fail();
                    }
                    finally
                    {
                        CloseableReference <CloseableImage> .CloseSafely(reference);
                        completion.Set();
                    }
                }
                else
                {
                    Assert.Fail();
                    completion.Set();
                }
            },
                response =>
            {
                Assert.Fail();
                completion.Set();
            });

            dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
            completion.WaitOne();
        }
示例#7
0
        public void TestFetchEncodedImageSuccess()
        {
            var completion     = new ManualResetEvent(false);
            var dataSource     = _imagePipeline.FetchEncodedImage(ImageRequest.FromUri(IMAGE_URL), null);
            var dataSubscriber = new BaseDataSubscriberImpl <CloseableReference <IPooledByteBuffer> >(
                async response =>
            {
                CloseableReference <IPooledByteBuffer> reference = response.GetResult();
                if (reference != null)
                {
                    IPooledByteBuffer inputStream = reference.Get();

                    try
                    {
                        Assert.IsTrue(inputStream.Size != 0);
                        Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
                    }
                    catch (Exception)
                    {
                        Assert.Fail();
                    }
                    finally
                    {
                        CloseableReference <IPooledByteBuffer> .CloseSafely(reference);
                        completion.Set();
                    }
                }
                else
                {
                    Assert.Fail();
                    completion.Set();
                }
            },
                response =>
            {
                Assert.Fail();
                completion.Set();
            });

            dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
            completion.WaitOne();
        }
示例#8
0
        public void TestFetchDecodedImageFail()
        {
            var completion     = new ManualResetEvent(false);
            var dataSource     = _imagePipeline.FetchDecodedImage(ImageRequest.FromUri(FAILURE_URL), null);
            var dataSubscriber = new BaseDataSubscriberImpl <CloseableReference <CloseableImage> >(
                response =>
            {
                Assert.Fail();
                completion.Set();
                return(Task.CompletedTask);
            },
                response =>
            {
                Assert.IsTrue(response.GetFailureCause().GetType() == typeof(IOException));
                completion.Set();
            });

            dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
            completion.WaitOne();
        }
示例#9
0
        public async Task TestEviction()
        {
            // Fetch a decoded image
            await _imagePipeline.FetchDecodedBitmapImageAsync(
                ImageRequest.FromUri(IMAGE_URL)).ConfigureAwait(false);

            Assert.IsTrue(_imagePipeline.IsInBitmapMemoryCache(IMAGE_URL));
            Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));

            // Evict from memory cache
            _imagePipeline.EvictFromMemoryCache(IMAGE_URL);
            Assert.IsFalse(_imagePipeline.IsInBitmapMemoryCache(IMAGE_URL));
            Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));

            // Evict from disk cache
            await _imagePipeline.EvictFromDiskCacheAsync(IMAGE_URL).ConfigureAwait(false);

            Assert.IsFalse(_imagePipeline.IsInBitmapMemoryCache(IMAGE_URL));
            Assert.IsFalse(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
        }
示例#10
0
        public async Task TestCancellingPrefetchToDiskCache1()
        {
            var failed         = false;
            var dataSource     = _imagePipeline.PrefetchToDiskCache(ImageRequest.FromUri(IMAGE4_URL), null);
            var dataSubscriber = new BaseDataSubscriberImpl <object>(
                response =>
            {
                failed = true;
                return(Task.CompletedTask);
            },
                response =>
            {
                failed = true;
            });

            dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
            dataSource.Close();
            Assert.IsFalse(failed);
            Assert.IsFalse(await _imagePipeline.IsInDiskCacheAsync(IMAGE4_URL).ConfigureAwait(false));
        }
示例#11
0
        public async Task TestCancellingFetchEncodedImage1()
        {
            var failed         = false;
            var dataSource     = _imagePipeline.FetchEncodedImage(ImageRequest.FromUri(IMAGE3_URL), null);
            var dataSubscriber = new BaseDataSubscriberImpl <CloseableReference <IPooledByteBuffer> >(
                response =>
            {
                failed = true;
                return(Task.CompletedTask);
            },
                response =>
            {
                failed = true;
            });

            dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
            dataSource.Close();
            Assert.IsFalse(failed);
            Assert.IsFalse(await _imagePipeline.IsInDiskCacheAsync(IMAGE3_URL).ConfigureAwait(false));
        }
示例#12
0
        public void TestFetchSendsSuccessToCallbackAfterRedirect2()
        {
            ManualResetEvent completion = new ManualResetEvent(false);
            bool             failed     = false;

            _producerContext = new SettableProducerContext(
                ImageRequest.FromUri(SUCCESS_REDIRECT_URL2),
                SUCCESS_REDIRECT_URL2,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _fetchState = new FetchState(_consumer, _producerContext);
            _fetcher.Fetch(
                _fetchState,
                new NetworkFetcherCallbackImpl(
                    (response, responseLength) =>
            {
                Assert.IsTrue(response != null);
                Assert.IsTrue(responseLength == -1);
                completion.Set();
            },
                    (throwable) =>
            {
                failed = true;
                completion.Set();
            },
                    () =>
            {
                failed = true;
                completion.Set();
            }));

            // Wait for callback
            completion.WaitOne();
            Assert.IsFalse(failed);
        }
示例#13
0
        public void TestFetchSendsErrorToCallbackAfterHttpError()
        {
            ManualResetEvent completion = new ManualResetEvent(false);
            bool             failed     = false;

            _producerContext = new SettableProducerContext(
                ImageRequest.FromUri(FAILURE_URL),
                FAILURE_URL,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);
            _fetchState = new FetchState(_consumer, _producerContext);
            _fetcher.Fetch(
                _fetchState,
                new NetworkFetcherCallbackImpl(
                    (response, responseLength) =>
            {
                failed = true;
                completion.Set();
            },
                    (throwable) =>
            {
                Assert.IsTrue(throwable.GetType() == typeof(IOException));
                completion.Set();
            },
                    () =>
            {
                failed = true;
                completion.Set();
            }));

            // Wait for callback
            completion.WaitOne();
            Assert.IsFalse(failed);
        }
        /// <summary>
        /// Gets the file cache path.
        /// </summary>
        /// <param name="uri">The image uri.</param>
        public Task <FileInfo> GetFileCachePath(Uri uri)
        {
            ICacheKey cacheKey = _cacheKeyFactory.GetEncodedCacheKey(
                ImageRequest.FromUri(uri), null);

            Task writeTask = _mainBufferedDiskCache.GetWriteToDiskCacheTask(cacheKey);

            if (writeTask != default(Task))
            {
                return(writeTask.ContinueWith(
                           task =>
                {
                    IBinaryResource resource = ImagePipelineFactory.Instance.GetMainDiskStorageCache().GetResource(cacheKey);
                    return ((FileBinaryResource)resource).File;
                },
                           TaskContinuationOptions.ExecuteSynchronously));
            }
            else
            {
                return(_mainBufferedDiskCache.Contains(cacheKey).ContinueWith(
                           task =>
                {
                    bool fileExists = task.Result;
                    if (fileExists)
                    {
                        IBinaryResource resource = ImagePipelineFactory.Instance.GetMainDiskStorageCache().GetResource(cacheKey);
                        return ((FileBinaryResource)resource).File;
                    }
                    else
                    {
                        return default(FileInfo);
                    }
                },
                           TaskContinuationOptions.ExecuteSynchronously));
            }
        }
 /// <summary>
 /// <para />If you have supplied your own cache key factory
 /// when configuring the pipeline, this method may not work
 /// correctly.
 /// It will only work if the custom factory builds the cache
 /// key entirely from the URI. If that is not the case, use
 /// EvictFromDiskCache(ImageRequest).
 /// </summary>
 /// <param name="uri">The uri of the image to evict.</param>
 public Task EvictFromDiskCacheAsync(Uri uri)
 {
     return(EvictFromDiskCacheAsync(ImageRequest.FromUri(uri)));
 }
        /// <summary>
        /// Fetches the encoded BitmapImage.
        /// </summary>
        /// <param name="uri">The image uri.</param>
        /// <param name="token">The cancellation token.</param>
        /// <param name="dispatcher">
        /// The current view's dispatcher, used to create BitmapImage.
        /// </param>
        /// <returns>The encoded BitmapImage.</returns>
        /// <exception cref="IOException">
        /// If the image uri can't be found.
        /// </exception>
        public Task <BitmapImage> FetchEncodedBitmapImageAsync(
            Uri uri,
            CancellationToken token   = default(CancellationToken),
            CoreDispatcher dispatcher = null)
        {
            var taskCompletionSource = new TaskCompletionSource <BitmapImage>();
            var dataSource           = FetchEncodedImage(ImageRequest.FromUri(uri), null);
            var dataSubscriber       = new BaseDataSubscriberImpl <CloseableReference <IPooledByteBuffer> >(
                async response =>
            {
                CloseableReference <IPooledByteBuffer> reference = response.GetResult();
                if (reference != null)
                {
                    //----------------------------------------------------------------------
                    // Phong Cao: InMemoryRandomAccessStream can't write anything < 16KB.
                    // http://stackoverflow.com/questions/25928408/inmemoryrandomaccessstream-incorrect-behavior
                    //----------------------------------------------------------------------
                    IPooledByteBuffer inputStream = reference.Get();
                    int supportedSize             = Math.Max(16 * ByteConstants.KB, inputStream.Size);

                    // Allocate temp buffer for stream convert
                    byte[] bytesArray = default(byte[]);
                    CloseableReference <byte[]> bytesArrayRef = default(CloseableReference <byte[]>);

                    try
                    {
                        bytesArrayRef = _flexByteArrayPool.Get(supportedSize);
                        bytesArray    = bytesArrayRef.Get();
                    }
                    catch (Exception)
                    {
                        // Allocates the byte array since the pool couldn't provide one
                        bytesArray = new byte[supportedSize];
                    }

                    try
                    {
                        inputStream.Read(0, bytesArray, 0, inputStream.Size);
                        await DispatcherHelpers.CallOnDispatcherAsync(async() =>
                        {
                            using (var outStream = new InMemoryRandomAccessStream())
                                using (var writeStream = outStream.AsStreamForWrite())
                                {
                                    await writeStream.WriteAsync(bytesArray, 0, supportedSize);
                                    outStream.Seek(0);
                                    BitmapImage bitmapImage = new BitmapImage();
                                    await bitmapImage.SetSourceAsync(outStream).AsTask().ConfigureAwait(false);
                                    taskCompletionSource.SetResult(bitmapImage);
                                }
                        },
                                                                      dispatcher).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        taskCompletionSource.SetException(e);
                    }
                    finally
                    {
                        CloseableReference <IPooledByteBuffer> .CloseSafely(reference);
                        CloseableReference <byte[]> .CloseSafely(bytesArrayRef);
                    }
                }
                else
                {
                    taskCompletionSource.SetResult(null);
                }
            },
                response =>
            {
                taskCompletionSource.SetException(response.GetFailureCause());
            });

            dataSource.Subscribe(dataSubscriber, _handleResultExecutor);
            token.Register(() =>
            {
                dataSource.Close();
                taskCompletionSource.TrySetCanceled();
            });

            return(taskCompletionSource.Task);
        }
 /// <summary>
 /// Returns whether the image is stored in the disk cache.
 /// </summary>
 /// <param name="uri">
 /// The uri for the image to be looked up.
 /// </param>
 /// <returns>
 /// true if the image was found in the disk cache,
 /// false otherwise.
 /// </returns>
 public Task <bool> IsInDiskCacheAsync(Uri uri)
 {
     return(IsInDiskCacheAsync(ImageRequest.FromUri(uri)));
 }
 /// <summary>
 /// Returns whether the image is stored in the disk cache.
 ///
 /// <para />If you have supplied your own cache key factory
 /// when configuring the pipeline, this method may not work
 /// correctly.
 /// It will only work if the custom factory builds the cache
 /// key entirely from the URI. If that is not the case, use
 /// IsInDiskCache(ImageRequest).
 /// </summary>
 /// <param name="uri">
 /// The uri for the image to be looked up.
 /// </param>
 /// <returns>
 /// true if the image was found in the disk cache,
 /// false otherwise.
 /// </returns>
 public IDataSource <bool> IsInDiskCache(Uri uri)
 {
     return(IsInDiskCache(ImageRequest.FromUri(uri)));
 }