Convert HEIC images before rendering them
This commit is contained in:
parent
ca18d9bc63
commit
1c42e32adf
2 changed files with 39 additions and 11 deletions
|
@ -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();
|
||||
if (extension(title) == ".HEIC" || quality != 100) {
|
||||
return asset.originBytes.then((bytes) =>
|
||||
FlutterImageCompress.compressWithList(bytes, quality: quality)
|
||||
.then((result) => Uint8List.fromList(result)));
|
||||
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 {
|
||||
return asset.originBytes;
|
||||
dataFuture = asset.originBytes;
|
||||
}
|
||||
if (extension(title) == ".HEIC" || quality != 100) {
|
||||
return dataFuture.then((bytes) =>
|
||||
FlutterImageCompress.compressWithList(bytes, quality: quality)
|
||||
.then((result) {
|
||||
log("Time taken: " +
|
||||
DateTime.now().difference(start).inMilliseconds.toString());
|
||||
return Uint8List.fromList(result);
|
||||
}));
|
||||
} else {
|
||||
return dataFuture;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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(() {
|
||||
|
|
Loading…
Add table
Reference in a new issue