CISourceOverCompositing 在设备和模拟器中产生不同的结果

发布于 2024-12-28 11:41:25 字数 973 浏览 1 评论 0原文

我正在尝试使用 CISourceOverCompositing 过滤器,但遇到了困难。

这是相关代码。 mask 是一个 UIImage , images 是一个 UIImage 数组

ci_mask = [[CIImage alloc] initWithCGImage: mask.CGImage];
ctx = [CIContext contextWithOptions: nil];
compo = [CIFilter filterWithName: @"CISourceOverCompositing"];
for(int i = 0; i < images.count; i++) {
  UIImage *image = [images objectAtIndex: i];
  ci_base = [[CIImage alloc] initWithCGImage: image.CGImage];
  [compo setDefaults];
  [compo setValue: ci_mask forKey: @"inputImage"];
  [compo setValue: ci_base forKey: @"inputBackgroundImage"];
  result = compo.outputImage;

  CGImageDestinationAddImage(
    dst_ref,
    [ctx createCGImage: result fromRect:result.extent],
    frame_props
  );
}

mask 包含一个 Alpha 通道,该通道在模拟器中正确应用,但在设备上却没有。输出仅按原样显示蒙版,而没有使用 Alpha 通道来混合图像。

使用 CoreGraphics API 的几乎相同的代码工作得很好(但是我不能应用其他 CIFilters)

我可能会尝试使用 CIBlendWithMask 但随后我必须提取蒙版并增加复杂性。 。

I'm trying to use the CISourceOverCompositing filter but I'm hitting some wall.

This is the relevant code. mask is a UIImage and images is an array of UIImage

ci_mask = [[CIImage alloc] initWithCGImage: mask.CGImage];
ctx = [CIContext contextWithOptions: nil];
compo = [CIFilter filterWithName: @"CISourceOverCompositing"];
for(int i = 0; i < images.count; i++) {
  UIImage *image = [images objectAtIndex: i];
  ci_base = [[CIImage alloc] initWithCGImage: image.CGImage];
  [compo setDefaults];
  [compo setValue: ci_mask forKey: @"inputImage"];
  [compo setValue: ci_base forKey: @"inputBackgroundImage"];
  result = compo.outputImage;

  CGImageDestinationAddImage(
    dst_ref,
    [ctx createCGImage: result fromRect:result.extent],
    frame_props
  );
}

mask contains an alpha channel which is correctly applied in the simulator but not on the device. The output only show the mask as-is seemingly without using the alpha channel to blend images.

The almost same code using CoreGraphics API works fine (but then I can't apply other CIFilters)

I'll probably try to use CIBlendWithMask but then I'll have to extract the mask and add complexity...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2025-01-04 11:41:25

查找文件名和指定文件中大小写不同的情况。它们不必是相同的大小写即可在模拟器中工作,但它们在设备上区分大小写。这让我多次感到困惑,如果你不寻找,就很难找到。

Look for different capitalization in your filenames and the files being specified. They don't have to be the same case to work in the simulator, but they are case sensitive on the device. This has thrown me off many times and if you don't look for it is quite difficult to track down.

您的好友蓝忘机已上羡 2025-01-04 11:41:25

好的,我发现了这个问题,但有点棘手。首先,要回答 Jeshua,掩码和基础都是生成的,因此路径与此处无关(但我会记住这一点,绝对很高兴知道)。

现在来说说“解决方案”。生成蒙版时,我在后台上下文上使用了 CG* 调用的组合(CGImageCreateWithMask,...)。现在,这些调用的结果似乎给了我一个似乎没有 Alpha 通道的 CGImageCGImageGetAlphaInfo 返回 0),但是......设备上和模拟器中的 CoreGraphics API和 CoreImage API,但仅在模拟器中应用仍然存在的 Alpha 通道。

使用 kCGImageAlphaPremultipliedLast 创建 CGContext 并使用 CGContextDrawImagekCGBlendModeSourceOut(或任何您需要“挖空”图像的内容)可以保持 Alpha 通道完好无损,并且这适用于模拟器和设备。

我将归档雷达,因为模拟器或设备有问题。

OK I found the issue and it's a bit tricky. First, to answer Jeshua, both the mask and the base are generated so the path isn't relevant here (but I'll keep that in mind, definitively good to know).

Now for the "solution". When generating the mask I used a combination of CG* calls on a background context (CGImageCreateWithMask, ...). Now, it seems that the result of those call gives me a CGImage seemingly without alpha channel (CGImageGetAlphaInfo returns 0) but... both CoreGraphics APIs on device and in the simulator AND CoreImage APIs but only in the simulator applies the still present alpha channel.

Creating a CGContext with kCGImageAlphaPremultipliedLast and using CGContextDrawImage with kCGBlendModeSourceOut (or whatever you need to "hollow out" your image) keeps the alpha channel intact and this works on both the simulator and the device.

I'll file a radar as either the simulator or the device is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文