@@ -609,6 +609,15 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
609
609
if (options[SDImageCoderEncodeCompressionQuality]) {
610
610
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue ];
611
611
}
612
+ CGSize maxPixelSize = CGSizeZero;
613
+ NSValue *maxPixelSizeValue = options[SDImageCoderEncodeMaxPixelSize];
614
+ if (maxPixelSizeValue != nil ) {
615
+ #if SD_MAC
616
+ maxPixelSize = maxPixelSizeValue.sizeValue ;
617
+ #else
618
+ maxPixelSize = maxPixelSizeValue.CGSizeValue ;
619
+ #endif
620
+ }
612
621
NSUInteger maxFileSize = 0 ;
613
622
if (options[SDImageCoderEncodeMaxFileSize]) {
614
623
maxFileSize = [options[SDImageCoderEncodeMaxFileSize] unsignedIntegerValue ];
@@ -618,7 +627,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
618
627
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue ];
619
628
if (encodeFirstFrame || frames.count == 0 ) {
620
629
// for static single webp image
621
- data = [self sd_encodedWebpDataWithImage: image.CGImage quality: compressionQuality fileSize : maxFileSize];
630
+ data = [self sd_encodedWebpDataWithImage: image.CGImage quality: compressionQuality maxPixelSize: maxPixelSize maxFileSize : maxFileSize];
622
631
} else {
623
632
// for animated webp image
624
633
WebPMux *mux = WebPMuxNew ();
@@ -627,7 +636,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
627
636
}
628
637
for (size_t i = 0 ; i < frames.count ; i++) {
629
638
SDImageFrame *currentFrame = frames[i];
630
- NSData *webpData = [self sd_encodedWebpDataWithImage: currentFrame.image.CGImage quality: compressionQuality fileSize : maxFileSize];
639
+ NSData *webpData = [self sd_encodedWebpDataWithImage: currentFrame.image.CGImage quality: compressionQuality maxPixelSize: maxPixelSize maxFileSize : maxFileSize];
631
640
int duration = currentFrame.duration * 1000 ;
632
641
WebPMuxFrameInfo frame = { .bitstream .bytes = webpData.bytes ,
633
642
.bitstream .size = webpData.length ,
@@ -664,7 +673,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
664
673
return data;
665
674
}
666
675
667
- - (nullable NSData *)sd_encodedWebpDataWithImage : (nullable CGImageRef)imageRef quality : (double )quality fileSize : ( NSUInteger )fileSize {
676
+ - (nullable NSData *)sd_encodedWebpDataWithImage : (nullable CGImageRef)imageRef quality : (double )quality maxPixelSize : (CGSize) maxPixelSize maxFileSize : ( NSUInteger )maxFileSize {
668
677
NSData *webpData;
669
678
if (!imageRef) {
670
679
return nil ;
@@ -780,11 +789,11 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
780
789
return nil ;
781
790
}
782
791
783
- config.target_size = (int )fileSize ; // Max filesize for output, 0 means use quality instead
784
- config.pass = fileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
792
+ config.target_size = (int )maxFileSize ; // Max filesize for output, 0 means use quality instead
793
+ config.pass = maxFileSize > 0 ? 6 : 1 ; // Use 6 passes for file size limited encoding, which is the default value of `cwebp` command line
785
794
config.thread_level = 1 ; // Thread encoding for fast
786
795
config.lossless = 0 ; // Disable lossless encoding (If we need, can add new Encoding Options in future version)
787
- picture.use_argb = config. lossless ; // Lossy encoding use YUV for internel bitstream
796
+ picture.use_argb = 0 ; // Lossy encoding use YUV for internel bitstream
788
797
picture.width = (int )width;
789
798
picture.height = (int )height;
790
799
picture.writer = WebPMemoryWrite; // Output in memory data buffer
@@ -803,9 +812,21 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef q
803
812
return nil ;
804
813
}
805
814
815
+ // Check if need to scale pixel size
816
+ if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && width > maxPixelSize.width && height > maxPixelSize.height ) {
817
+ CGSize scaledSize = SDCalculateThumbnailSize (CGSizeMake (width, height), YES , maxPixelSize);
818
+ result = WebPPictureRescale (&picture, scaledSize.width , scaledSize.height );
819
+ if (!result) {
820
+ WebPMemoryWriterClear (&writer);
821
+ WebPPictureFree (&picture);
822
+ CFRelease (dataRef);
823
+ return nil ;
824
+ }
825
+ }
826
+
806
827
result = WebPEncode (&config, &picture);
807
- CFRelease (dataRef); // Free bitmap buffer
808
828
WebPPictureFree (&picture);
829
+ CFRelease (dataRef); // Free bitmap buffer
809
830
810
831
if (result) {
811
832
// success
0 commit comments