YYImage icon indicating copy to clipboard operation
YYImage copied to clipboard

加载webp图片颜色显示不对

Open DoromGeng opened this issue 1 year ago • 1 comments

问题:图片背景鲜红色变成了暗红色 图片链接:https://postimg.cc/FYJ7ysH5/1106a583 效果对比 原始图 8f75540f-5f98-4c13-ac66-3cd07ca0bd90 YY加载图 image

DoromGeng avatar Jan 18 '24 02:01 DoromGeng

一、添加新方法 `- (nonnull CGColorSpaceRef)createColorSpaceWithDemuxer:(nonnull WebPDemuxer *)demuxer CF_RETURNS_RETAINED {
CGColorSpaceRef colorSpaceRef = NULL; uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS);

if (flags & ICCP_FLAG) {
    WebPChunkIterator chunk_iter;
    int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter);
    if (result) {
        // See #2618, the `CGColorSpaceCreateWithICCProfile` does not copy ICC Profile data, it only retain `CFDataRef`.
        // When the libwebp `WebPDemuxer` dealloc, all chunks will be freed. So we must copy the ICC data (really cheap, less than 10KB)
        NSData *profileData = [NSData dataWithBytes:chunk_iter.chunk.bytes length:chunk_iter.chunk.size];
        if (@available(iOS 10, tvOS 10, macOS 10.12, watchOS 3, *)) {
            colorSpaceRef = CGColorSpaceCreateWithICCData((__bridge CFDataRef)profileData);
        } else {
            colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData);
        }
        WebPDemuxReleaseChunkIterator(&chunk_iter);
        if (colorSpaceRef) {
            // We use RGB color model to decode WebP images currently, so we must filter out other colorSpace
            CGColorSpaceModel model = CGColorSpaceGetModel(colorSpaceRef);
            if (model != kCGColorSpaceModelRGB) {
                CGColorSpaceRelease(colorSpaceRef);
                colorSpaceRef = NULL;
            }
        }
    }
}

return colorSpaceRef;

} `

二、在2195行修改为如下代码-使用webp本身的颜色空间

CGColorSpaceRef colorspce = [self createColorSpaceWithDemuxer:_webpSource]; if(!colorspce) { colorspce = YYCGColorSpaceGetDeviceRGB(); } CGImageRef image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspce, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault);

DoromGeng avatar Jan 18 '24 03:01 DoromGeng