diff --git a/lib/core/cache/image_cache.dart b/lib/core/cache/image_cache.dart index 73750c11d..8d63b69fd 100644 --- a/lib/core/cache/image_cache.dart +++ b/lib/core/cache/image_cache.dart @@ -1,9 +1,10 @@ import 'dart:io'; +import 'dart:typed_data'; import 'package:photos/core/cache/lru_map.dart'; import 'package:photos/models/photo.dart'; -class ImageLruCache { +class FileLruCache { static LRUMap _map = LRUMap(25); static File get(Photo photo) { @@ -14,3 +15,15 @@ class ImageLruCache { _map.put(photo.hashCode, imageData); } } + +class BytesLruCache { + static LRUMap _map = LRUMap(25); + + static Uint8List get(Photo photo) { + return _map.get(photo.hashCode); + } + + static void put(Photo photo, Uint8List imageData) { + _map.put(photo.hashCode, imageData); + } +} diff --git a/lib/ui/zoomable_image.dart b/lib/ui/zoomable_image.dart index 6d3ed9f91..dc9016dea 100644 --- a/lib/ui/zoomable_image.dart +++ b/lib/ui/zoomable_image.dart @@ -110,13 +110,31 @@ class _ZoomableImageState extends State void _loadNetworkImage() { if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) { - _imageProvider = Image.network(widget.photo.getThumbnailUrl()).image; + _imageProvider = Image.network( + widget.photo.getThumbnailUrl(), + gaplessPlayback: true, + ).image; _loadedSmallThumbnail = true; } if (!_loadedFinalImage) { - widget.photo.getBytes().then((data) { - _onFinalImageLoaded(Image.memory(data).image, context); - }); + if (BytesLruCache.get(widget.photo) != null) { + _onFinalImageLoaded( + Image.memory( + BytesLruCache.get(widget.photo), + gaplessPlayback: true, + ).image, + context); + } else { + widget.photo.getBytes().then((data) { + _onFinalImageLoaded( + Image.memory( + data, + gaplessPlayback: true, + ).image, + context); + BytesLruCache.put(widget.photo, data); + }); + } } } @@ -154,7 +172,7 @@ class _ZoomableImageState extends State if (!_loadingFinalImage && !_loadedFinalImage) { _loadingFinalImage = true; - final cachedFile = ImageLruCache.get(widget.photo); + final cachedFile = FileLruCache.get(widget.photo); if (cachedFile != null) { final imageProvider = Image.file(cachedFile).image; _onFinalImageLoaded(imageProvider, context); @@ -164,7 +182,7 @@ class _ZoomableImageState extends State if (mounted) { final imageProvider = Image.file(file).image; _onFinalImageLoaded(imageProvider, context); - ImageLruCache.put(widget.photo, file); + FileLruCache.put(widget.photo, file); } }); });