我找到了一个关于组合两个图像以创建一个可以在此响应中共享和保存的新图像的答案:Stack response on how to combine two images swiftui (swift 5 answer by Glen) 。我对如何在代码中使用它感到困惑,因为我不熟悉 SwiftUI/UIKit 交叉。您能展示一个如何使用 UIImage 扩展来组合两个图像的示例吗?
extension UIImage {
func mergeWith(topImage: UIImage) -> UIImage {
let bottomImage = self
UIGraphicsBeginImageContext(size)
let areaSize = CGRect(x: 0, y: 0, width: bottomImage.size.width, height: bottomImage.size.height)
bottomImage.draw(in: areaSize)
topImage.draw(in: areaSize, blendMode: .normal, alpha: 1.0)
let mergedImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return mergedImage
}
}
最佳答案
引用Swift docs在这里。
Extensions add new functionality to an existing class, structure, enumeration, or protocol type.
您将使用如下扩展,
let image1 = UIImage(named: "yourimage1")
let image2 = UIImage(named: "yourimage2")
let finalImage = image1.mergeWith(image2)
//Use finalImage for sharing or saving..
关于swift - 组合图像 SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64887200/