Browse Source

Convert HEIC images before rendering them

Vishnu Mohandas 5 years ago
parent
commit
1c42e32adf
2 changed files with 37 additions and 9 deletions
  1. 22 3
      lib/models/photo.dart
  2. 15 6
      lib/ui/zoomable_image.dart

+ 22 - 3
lib/models/photo.dart

@@ -1,5 +1,8 @@
+import 'dart:developer';
+import 'dart:io';
 import 'dart:typed_data';
 
+import 'package:flutter/foundation.dart';
 import 'package:flutter_image_compress/flutter_image_compress.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:path/path.dart';
@@ -58,12 +61,28 @@ class Photo {
 
   Future<Uint8List> getBytes({int quality = 100}) {
     final asset = getAsset();
+    Future<Uint8List> dataFuture;
+    final start = DateTime.now();
+    if (localId == null) {
+      dataFuture =
+          HttpClient().getUrl(Uri.parse(getRemoteUrl())).then((request) {
+        return request.close().then((response) {
+          return consolidateHttpClientResponseBytes(response);
+        });
+      });
+    } else {
+      dataFuture = asset.originBytes;
+    }
     if (extension(title) == ".HEIC" || quality != 100) {
-      return asset.originBytes.then((bytes) =>
+      return dataFuture.then((bytes) =>
           FlutterImageCompress.compressWithList(bytes, quality: quality)
-              .then((result) => Uint8List.fromList(result)));
+              .then((result) {
+            log("Time taken: " +
+                DateTime.now().difference(start).inMilliseconds.toString());
+            return Uint8List.fromList(result);
+          }));
     } else {
-      return asset.originBytes;
+      return dataFuture;
     }
   }
 

+ 15 - 6
lib/ui/zoomable_image.dart

@@ -1,8 +1,8 @@
 import 'dart:io';
+import 'package:flutter/foundation.dart';
 import 'package:flutter/widgets.dart';
 import 'package:photos/core/cache/image_cache.dart';
 import 'package:photos/core/cache/thumbnail_cache.dart';
-import 'package:photos/core/configuration.dart';
 import 'package:photos/models/photo.dart';
 import 'package:photos/ui/loading_widget.dart';
 import 'package:photo_view/photo_view.dart';
@@ -60,7 +60,15 @@ class _ZoomableImageState extends State<ZoomableImage> {
   }
 
   void _loadNetworkImage() {
-    _imageProvider = Image.network(widget.photo.getRemoteUrl()).image;
+    if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) {
+      _imageProvider = Image.network(widget.photo.getThumbnailUrl()).image;
+      _loadedSmallThumbnail = true;
+    }
+    if (!_loadedFinalImage) {
+      widget.photo.getBytes().then((data) {
+        _onFinalImageLoaded(Image.memory(data).image, context);
+      });
+    }
   }
 
   void _loadLocalImage(BuildContext context) {
@@ -94,12 +102,14 @@ class _ZoomableImageState extends State<ZoomableImage> {
     if (!_loadedFinalImage) {
       final cachedFile = ImageLruCache.get(widget.photo);
       if (cachedFile != null) {
-        _onFinalImageLoaded(cachedFile, context);
+        final imageProvider = Image.file(cachedFile).image;
+        _onFinalImageLoaded(imageProvider, context);
       } else {
         widget.photo.getAsset().file.then((file) {
           if (mounted) {
             setState(() {
-              _onFinalImageLoaded(file, context);
+              final imageProvider = Image.file(file).image;
+              _onFinalImageLoaded(imageProvider, context);
               ImageLruCache.put(widget.photo, file);
             });
           }
@@ -120,8 +130,7 @@ class _ZoomableImageState extends State<ZoomableImage> {
     });
   }
 
-  void _onFinalImageLoaded(File file, BuildContext context) {
-    final imageProvider = Image.file(file).image;
+  void _onFinalImageLoaded(ImageProvider imageProvider, BuildContext context) {
     precacheImage(imageProvider, context).then((value) {
       if (mounted) {
         setState(() {