在像Photoshop这样的UIImageViews上设置blendmodes

问题描述 投票:3回答:2

我一直在尝试将混合模式应用到我的UIImageViews来复制PSD模拟文件(抱歉无法提供)。 PSD文件有3层,基色为60%普通混合,图像层为55%多重混合,梯度层为35%叠加。

我一直在互联网上尝试几个教程,但仍然无法使颜色/图像完全相同。

我注意到的一件事是我的iPhone的颜色与我的Mac屏幕不同。

我找到了Quartz 2D的文档,我认为这是正确的方法,但我无法获得有关使用图像混合模式的任何示例/教程。 https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBIJEFG

任何人都可以提供一个好的教程,与文档中的那个教程相同,所以如果没有人给我一个直截了当的答案,我可以至少尝试混淆。

ios quartz-2d
2个回答
0
投票

你不想为此使用UIImageView,因为它实际上不支持混合模式。

相反,要走的路是创建一个UIView子类(它的行为就像UIImageView一样)。因此,创建一个名为BlendedImageView的UIView子类。它应该有一个image属性,然后在drawRect:方法中你可以这样做:

- (void)drawRect:(CGRect)rect
{
    //
    // Here you need to calculate a rect based on self.contentMode to replicate UIImageView-behaviour.
    // For example, for UIViewContentModeCenter, you want a rect that goes outside 'rect' and centers with it.
    //
    CGRect actualRect = // ...
    [self.image drawInRect:actualRect blendMode:kCGBlendModeOverlay alpha:0.5];
}

替换alpha和混合模式以符合您的偏好。好的做法是让BlendedImageView保存一个blendMode属性。

在您的情况下,您可能需要使用与上面相同的代码来绘制所有3个图像的更高级视图。


0
投票

很久以前就提出过这个问题,但如果有人在寻找相同的答案,你可以在视图的后备层上使用compositingFilter来获得你想要的东西:

overlayView.layer.compositingFilter = "multiplyBlendMode"

由@SeanA建议,在这里:https://stackoverflow.com/a/46676507/1147286

过滤器 完整的过滤器列表是here 或者您可以打印出合成过滤器类型

print("Filters:\n",CIFilter.filterNames(inCategory: kCICategoryCompositeOperation))
© www.soinside.com 2019 - 2024. All rights reserved.