【发布时间】:2020-11-18 04:40:37
【问题描述】:
我找到了一个关于合并两张图片以创建一个可以共享并保存在此回复中的新图片的答案: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
}
}
【问题讨论】: