@@ -766,11 +766,14 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
766766 if (!dataRef) {
767767 return nil ;
768768 }
769+ // Check colorSpace is RGB/RGBA
770+ CGColorSpaceRef colorSpace = CGImageGetColorSpace (imageRef);
771+ BOOL isRGB = CGColorSpaceGetModel (colorSpace) == kCGColorSpaceModelRGB ;
769772
770773 uint8_t *rgba = NULL ; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
771774 // 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 ;
774777 if (isRGB888 || isRGBA8888) {
775778 // If the input CGImage is already RGB888/RGBA8888
776779 rgba = (uint8_t *)CFDataGetBytePtr (dataRef);
@@ -782,7 +785,7 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
782785 vImage_CGImageFormat srcFormat = {
783786 .bitsPerComponent = (uint32_t )bitsPerComponent,
784787 .bitsPerPixel = (uint32_t )bitsPerPixel,
785- .colorSpace = CGImageGetColorSpace (imageRef) ,
788+ .colorSpace = colorSpace ,
786789 .bitmapInfo = bitmapInfo
787790 };
788791 vImage_CGImageFormat destFormat = {
0 commit comments