-
Notifications
You must be signed in to change notification settings - Fork 6k
Add more flexible image loading API #38905
Changes from all commits
759c980
b095f72
e6371f3
ca4dc78
1a026b3
5c7d830
cb7cf84
d31fa58
3d79727
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,6 +491,42 @@ Future<Codec> instantiateImageCodecFromBuffer( | |
targetHeight: targetHeight, | ||
allowUpscaling: allowUpscaling); | ||
|
||
Future<Codec> instantiateImageCodecWithSize( | ||
ImmutableBuffer buffer, { | ||
TargetImageSizeCallback? getTargetSize, | ||
}) async { | ||
if (getTargetSize == null) { | ||
return engine.renderer.instantiateImageCodec(buffer._list!); | ||
} else { | ||
final Codec codec = await engine.renderer.instantiateImageCodec(buffer._list!); | ||
try { | ||
final FrameInfo info = await codec.getNextFrame(); | ||
try { | ||
final int width = info.image.width; | ||
final int height = info.image.height; | ||
final TargetImageSize targetSize = getTargetSize(width, height); | ||
return engine.renderer.instantiateImageCodec(buffer._list!, | ||
targetWidth: targetSize.width, targetHeight: targetSize.height, allowUpscaling: false); | ||
} finally { | ||
info.image.dispose(); | ||
} | ||
} finally { | ||
codec.dispose(); | ||
} | ||
} | ||
} | ||
|
||
typedef TargetImageSizeCallback = TargetImageSize Function(int intrinsicWidth, int intrinsicHeight); | ||
|
||
class TargetImageSize { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there other places in dart:ui we could use an int-based size? Might make sense to generalise the name so others could use it later, if so. I can’t think of a great name though. Maybe something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, but a real There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More similar to an IntConstraints type, not quite a size. We do really need an ISize type, but probably unrelated. |
||
const TargetImageSize({this.width, this.height}) | ||
: assert(width == null || width > 0), | ||
assert(height == null || height > 0); | ||
|
||
final int? width; | ||
final int? height; | ||
} | ||
|
||
Future<Codec> webOnlyInstantiateImageCodecFromUrl(Uri uri, | ||
{engine.WebOnlyImageCodecChunkCallback? chunkCallback}) => | ||
engine.renderer.instantiateImageCodecFromUrl(uri, chunkCallback: chunkCallback); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe note here that image resizing capabilities are only available with the canvaskit renderer and not html renderer (we should probably update the api docs for
instantiateImageCodec
to note this too)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done