@@ -766,11 +766,14 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
766
766
if (!dataRef) {
767
767
return nil ;
768
768
}
769
+ // Check colorSpace is RGB/RGBA
770
+ CGColorSpaceRef colorSpace = CGImageGetColorSpace (imageRef);
771
+ BOOL isRGB = CGColorSpaceGetModel (colorSpace) == kCGColorSpaceModelRGB ;
769
772
770
773
uint8_t *rgba = NULL ; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
771
774
// We could not assume that input CGImage's color mode is always RGB888/RGBA8888. Convert all other cases to target color mode using vImage
772
- BOOL isRGB888 = byteOrderNormal && alphaInfo == kCGImageAlphaNone && components == 3 ;
773
- BOOL isRGBA8888 = byteOrderNormal && alphaInfo == kCGImageAlphaLast && components == 4 ;
775
+ BOOL isRGB888 = isRGB && byteOrderNormal && alphaInfo == kCGImageAlphaNone && components == 3 ;
776
+ BOOL isRGBA8888 = isRGB && byteOrderNormal && alphaInfo == kCGImageAlphaLast && components == 4 ;
774
777
if (isRGB888 || isRGBA8888) {
775
778
// If the input CGImage is already RGB888/RGBA8888
776
779
rgba = (uint8_t *)CFDataGetBytePtr (dataRef);
@@ -782,7 +785,7 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
782
785
vImage_CGImageFormat srcFormat = {
783
786
.bitsPerComponent = (uint32_t )bitsPerComponent,
784
787
.bitsPerPixel = (uint32_t )bitsPerPixel,
785
- .colorSpace = CGImageGetColorSpace (imageRef) ,
788
+ .colorSpace = colorSpace ,
786
789
.bitmapInfo = bitmapInfo
787
790
};
788
791
vImage_CGImageFormat destFormat = {
0 commit comments