Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter of image bytes before decoding #660

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions cached_network_image/lib/src/cached_image_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class CachedNetworkImage extends StatelessWidget {
/// Will resize the image and store the resized image in the disk cache.
final int? maxHeightDiskCache;

final ImageBytesBeforeDecoding? beforeDecoding;

/// CachedNetworkImage shows a network image using a caching mechanism. It also
/// provides support for a placeholder, showing an error and fading into the
/// loaded image. Next to that it supports most features of a default Image
Expand Down Expand Up @@ -230,6 +232,7 @@ class CachedNetworkImage extends StatelessWidget {
this.maxHeightDiskCache,
ImageRenderMethodForWeb imageRenderMethodForWeb =
ImageRenderMethodForWeb.HtmlImage,
this.beforeDecoding,
}) : _image = CachedNetworkImageProvider(
imageUrl,
headers: httpHeaders,
Expand All @@ -238,6 +241,7 @@ class CachedNetworkImage extends StatelessWidget {
imageRenderMethodForWeb: imageRenderMethodForWeb,
maxWidth: maxWidthDiskCache,
maxHeight: maxHeightDiskCache,
beforeDecoding: beforeDecoding,
),
super(key: key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:cached_network_image_platform_interface'
show ImageLoader;
import 'package:cached_network_image_platform_interface'
'/cached_network_image_platform_interface.dart'
show ImageRenderMethodForWeb;
show ImageRenderMethodForWeb, ImageBytesBeforeDecoding;

/// ImageLoader class to load images on IO platforms.
class ImageLoader implements platform.ImageLoader {
Expand All @@ -27,6 +27,7 @@ class ImageLoader implements platform.ImageLoader {
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
ImageBytesBeforeDecoding? beforeDecoding,
) async* {
try {
assert(
Expand Down Expand Up @@ -56,6 +57,9 @@ class ImageLoader implements platform.ImageLoader {
if (result is FileInfo) {
var file = result.file;
var bytes = await file.readAsBytes();
if (beforeDecoding != null) {
bytes = await beforeDecoding(bytes, url);
}
var decoded = await decode(bytes);
yield decoded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async' show Future, StreamController;
import 'dart:ui' as ui show Codec;

import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart'
show ImageRenderMethodForWeb;
show ImageBytesBeforeDecoding, ImageRenderMethodForWeb;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
Expand Down Expand Up @@ -34,6 +34,7 @@ class CachedNetworkImageProvider
this.cacheManager,
this.cacheKey,
this.imageRenderMethodForWeb = ImageRenderMethodForWeb.HtmlImage,
this.beforeDecoding,
});

/// CacheManager from which the image files are loaded.
Expand Down Expand Up @@ -65,6 +66,8 @@ class CachedNetworkImageProvider
/// Render option for images on the web platform.
final ImageRenderMethodForWeb imageRenderMethodForWeb;

final ImageBytesBeforeDecoding? beforeDecoding;

@override
Future<CachedNetworkImageProvider> obtainKey(
ImageConfiguration configuration) {
Expand Down Expand Up @@ -107,6 +110,7 @@ class CachedNetworkImageProvider
errorListener,
imageRenderMethodForWeb,
() => PaintingBinding.instance?.imageCache?.evict(key),
beforeDecoding,
);
}

Expand Down
8 changes: 6 additions & 2 deletions cached_network_image/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ dependencies:
sdk: flutter
flutter_cache_manager: ^3.0.0
octo_image: ^1.0.0
cached_network_image_platform_interface: ^1.0.0
cached_network_image_web: ^1.0.0
#cached_network_image_platform_interface: ^1.0.0
cached_network_image_platform_interface:
path: ../cached_network_image_platform_interface/
#cached_network_image_web: ^1.0.0
cached_network_image_web:
path: ../cached_network_image_web/


dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library cached_network_image_platform_interface;

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
Expand All @@ -17,6 +18,12 @@ enum ImageRenderMethodForWeb {
HttpGet, // ignore: constant_identifier_names
}

///
typedef ImageBytesBeforeDecoding = Future<Uint8List> Function(
Uint8List bytes,
String url,
);

/// ImageLoader class to load images differently on various platforms.
class ImageLoader {
/// loads the images async and gives the resulted codecs on a Stream. The
Expand All @@ -33,6 +40,7 @@ class ImageLoader {
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
ImageBytesBeforeDecoding? beforeDecoding,
) {
throw UnimplementedError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void main() {
null,
null,
ImageRenderMethodForWeb.HttpGet,
() => {}),
() => {},
null),
throwsA(const TypeMatcher<UnimplementedError>()));
});
});
Expand Down
8 changes: 7 additions & 1 deletion cached_network_image_web/lib/cached_network_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'dart:ui' as ui;

import 'package:cached_network_image_platform_interface'
'/cached_network_image_platform_interface.dart' as platform
show ImageLoader;
show ImageBytesBeforeDecoding, ImageLoader;
import 'package:cached_network_image_platform_interface'
'/cached_network_image_platform_interface.dart'
show ImageRenderMethodForWeb;
Expand All @@ -27,6 +27,7 @@ class ImageLoader implements platform.ImageLoader {
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
platform.ImageBytesBeforeDecoding? beforeDecoding,
) {
switch (imageRenderMethodForWeb) {
case ImageRenderMethodForWeb.HttpGet:
Expand All @@ -41,6 +42,7 @@ class ImageLoader implements platform.ImageLoader {
headers,
errorListener,
evictImage,
beforeDecoding,
);
case ImageRenderMethodForWeb.HtmlImage:
return _loadAsyncHtmlImage(url, chunkEvents, decode).asStream();
Expand All @@ -58,6 +60,7 @@ class ImageLoader implements platform.ImageLoader {
Map<String, String>? headers,
Function()? errorListener,
Function() evictImage,
platform.ImageBytesBeforeDecoding? beforeDecoding,
) async* {
try {
await for (var result in cacheManager.getFileStream(url,
Expand All @@ -71,6 +74,9 @@ class ImageLoader implements platform.ImageLoader {
if (result is FileInfo) {
var file = result.file;
var bytes = await file.readAsBytes();
if (beforeDecoding != null) {
bytes = await beforeDecoding(bytes, url);
}
var decoded = await decode(bytes);
yield decoded;
}
Expand Down
4 changes: 3 additions & 1 deletion cached_network_image_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ dependencies:
flutter:
sdk: flutter
flutter_cache_manager: ^3.0.0
cached_network_image_platform_interface: ^1.0.0
#cached_network_image_platform_interface: ^1.0.0
cached_network_image_platform_interface:
path: ../cached_network_image_platform_interface/

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void main() {
null,
null,
ImageRenderMethodForWeb.HttpGet,
() => {});
() => {},
null);
expect(stream, isNotNull);
});
}
Expand Down