如何在Swift中合并2个pdf矢量图像资产

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

我有两个矢量图像。我要合并它们,以便点出现在第一张图像的顶部。常规png资产的解决方案不起作用,因为它们使用图像的实际大小,并且合并后的图像非常模糊。

我发现的一个解决方案是在第一个UIImageView上放置第二个UIImageView(相同大小,居中),但看起来确实很愚蠢。

我需要在应用程序内部进行此设置,因为我还希望使正方形的颜色不同(更改矢量的色彩),然后与黑色边框图像合并。这样我的项目中可以有3个资产(正方形,边框和点)。

enter image description hereenter image description here

swift image pdf uiimageview vector-graphics
2个回答
0
投票

[在UIImageView的扩展中找到了实现此目的的方法。惊讶于我在任何地方都找不到。

extension UIImageView {

    func mergeTwoPDF(one: UIImage, two: UIImage) {
        UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.main.scale)
        let areaSize = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        one.draw(in: areaSize, blendMode: .normal, alpha: 1.0)
        two.draw(in: areaSize, blendMode: .normal, alpha: 1.0)
        //If you want to merge more than 2 images, just add them to the func parameters and repeat the line above with them
        let mergedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        self.image = mergedImage
    }
}

0
投票

我建议使用子层:

["firstOne", "secondOne"].forEach {
let myLayer = CALayer()
let myImage = UIImage(named: $0)?.cgImage
myLayer.frame = myView.bounds
myLayer.contents = myImage
myView.layer.addSublayer(myLayer)
}

或创建自定义图层类

© www.soinside.com 2019 - 2024. All rights reserved.