File tree Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -10,4 +10,6 @@ class R {
10
10
11
11
static const String IMG_TRANSPARENT_BACKGROUND_PNG =
12
12
'img/transparent-background.png' ;
13
+
14
+ static const String IMG_ICON_WEBP = 'img/icon.webp' ;
13
15
}
Original file line number Diff line number Diff line change @@ -287,6 +287,18 @@ class _MyAppState extends State<MyApp> {
287
287
});
288
288
}
289
289
290
+ void _compressFromWebPImage () async {
291
+ // Converting webp to jpeg
292
+ final result = await FlutterImageCompress .compressAssetImage (
293
+ R .IMG_ICON_WEBP ,
294
+ );
295
+ if (result == null ) return ;
296
+ // Show result image
297
+ safeSetState (() {
298
+ provider = MemoryImage (typed_data.Uint8List .fromList (result));
299
+ });
300
+ }
301
+
290
302
@override
291
303
Widget build (BuildContext context) {
292
304
return MaterialApp (
@@ -372,6 +384,12 @@ class _MyAppState extends State<MyApp> {
372
384
onPressed: _compressAndroidWebpExample,
373
385
),
374
386
),
387
+ SliverToBoxAdapter (
388
+ child: TextButton (
389
+ child: Text ('Convert from webp format' ),
390
+ onPressed: _compressFromWebPImage,
391
+ ),
392
+ ),
375
393
],
376
394
),
377
395
floatingActionButton: FloatingActionButton (
Original file line number Diff line number Diff line change 4
4
5
5
#import " CompressHandler.h"
6
6
#import " UIImage+scale.h"
7
+ #import " UIImage+WebP.h"
7
8
#import " ImageCompressPlugin.h"
8
9
#import < SDWebImageWebPCoder/SDImageWebPCoder.h>
9
10
@@ -13,7 +14,7 @@ @implementation CompressHandler {
13
14
14
15
+ (NSData *)compressWithData : (NSData *)data minWidth : (int )minWidth minHeight : (int )minHeight quality : (int )quality
15
16
rotate : (int )rotate format : (int )format {
16
- UIImage *img = [[UIImage alloc ] initWithData: data];
17
+ UIImage *img = [self isWebP: data] ? [UIImage sd_imageWithWebPData: data] : [ [UIImage alloc ] initWithData: data];
17
18
return [CompressHandler compressWithUIImage: img minWidth: minWidth minHeight: minHeight quality: quality rotate: rotate format: format];
18
19
}
19
20
@@ -80,4 +81,13 @@ + (NSData *)compressDataWithImage:(UIImage *)image quality:(float)quality format
80
81
return data;
81
82
}
82
83
84
+ + (BOOL )isWebP : (NSData *)data {
85
+ if (data.length < 12 ) return false ;
86
+
87
+ NSData *riff = [data subdataWithRange: NSMakeRange (8 , 4 )];
88
+ NSString * format = [[NSString alloc ] initWithData: riff encoding: (NSASCIIStringEncoding)];
89
+
90
+ return [format isEqualToString: @" WEBP" ];
91
+ }
92
+
83
93
@end
You can’t perform that action at this time.
0 commit comments