可见工件使用硬光传输模式将两个UIImage与UIGraphicsContext合成

时间:2018-07-23 02:38:20

标签: ios composite uigraphicscontext

我正在使用UIGraphicsContext将两个UIImage合成在一起。

它在大多数情况下都有效,但是使用Hard Light CGBlendMode时,如果在Photoshop中使用Hard Light传输模式,我会得到一些奇怪的伪影。

我会尝试使用Core Image,但是我必须能够控制通过Hard Light传输的顶层的不透明度,并且据我所知这是不可能的。

以下是使用UIGraphicsContext创建的图像: enter image description here

此处在Photoshop中使用相同的图层和不透明度: enter image description here

这是基本图像: enter image description here

然后使用“硬光”以60%的不透明度合成该层: enter image description here

我尝试使用透明层将上下文插值质量设置为较高,但是没有任何改善。

我的代码如下。有谁知道如何解决此问题,或者知道与在Photoshop中获得相同结果的替代方法?

library(data.table)
dt <- data.table(df)
res_dt <- dt[ , .(results = my_fun(traffic)), by=.(hour, dow)]

1 个答案:

答案 0 :(得分:0)

此问题已解决:Higlight Artifacts When Using Hard Light Blend Mode

我只需要将基础层的alpha设置为0.99。

这是更新的代码:

public func compImagesWithImage(_ topImg: UIImage, blendMode: CGBlendMode, opacity: CGFloat) -> UIImage? {

    let baseImg = self

    if let cgImage = baseImg.cgImage {
        let baseW = CGFloat(baseImg.cgImage!.width as size_t)
        let baseH = CGFloat(baseImg.cgImage!.height as size_t)

        UIGraphicsBeginImageContext(CGSize(width: baseW, height: baseH))
        if let context = UIGraphicsGetCurrentContext() {
            context.saveGState();
            context.interpolationQuality = .high

            let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: baseH)
            context.concatenate(flipVertical)

            context.setAlpha(0.99)
            context.setBlendMode(CGBlendMode.normal)
            context.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: CGSize(width: CGFloat(baseW), height: CGFloat(baseH))))

            context.setAlpha(opacity)
            context.setBlendMode(blendMode)
            context.beginTransparencyLayer(auxiliaryInfo: nil)

            context.draw(topImg.cgImage!, in: CGRect(origin: CGPoint.zero, size: CGSize(width: CGFloat(baseW), height: CGFloat(baseH))))

            context.endTransparencyLayer()

            context.restoreGState();

            let finalImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return finalImage

        }
    }