Add some caching for iOS

This commit is contained in:
Vishnu Mohandas 2020-04-25 02:29:11 +05:30
parent 639c444a4e
commit 96c26227f7
2 changed files with 43 additions and 14 deletions

16
lib/core/image_cache.dart Normal file
View file

@ -0,0 +1,16 @@
import 'dart:typed_data';
import 'package:myapp/core/lru_map.dart';
import 'package:myapp/models/photo.dart';
class ImageLruCache {
static LRUMap<int, Uint8List> _map = LRUMap(500);
static Uint8List get(Photo photo) {
return _map.get(photo.generatedId);
}
static void put(Photo photo, Uint8List imageData) {
_map.put(photo.generatedId, imageData);
}
}

View file

@ -1,5 +1,8 @@
import 'dart:typed_data';
import 'package:flutter/widgets.dart';
import 'package:logger/logger.dart';
import 'package:myapp/core/image_cache.dart';
import 'package:myapp/core/image_cache.dart';
import 'package:myapp/core/lru_map.dart';
import 'package:myapp/core/thumbnail_cache.dart';
import 'package:myapp/models/photo.dart';
@ -59,21 +62,19 @@ class _ZoomableImageState extends State<ZoomableImage> {
}
if (!_loadedFinalImage) {
widget.photo.getBytes().then((bytes) {
if (mounted) {
setState(() {
final imageProvider = Image.memory(bytes).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedFinalImage = true;
});
}
if (ImageLruCache.get(widget.photo) != null) {
final bytes = ImageLruCache.get(widget.photo);
_onFinalImageLoaded(bytes, context);
} else {
widget.photo.getBytes().then((bytes) {
if (mounted) {
setState(() {
ImageLruCache.put(widget.photo, bytes);
_onFinalImageLoaded(bytes, context);
});
});
}
});
}
});
}
}
if (_imageProvider != null) {
@ -88,4 +89,16 @@ class _ZoomableImageState extends State<ZoomableImage> {
return loadWidget;
}
}
void _onFinalImageLoaded(Uint8List bytes, BuildContext context) {
final imageProvider = Image.memory(bytes).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedFinalImage = true;
});
}
});
}
}