【问题标题】:Combining images SwiftUI [closed]组合图像 SwiftUI [关闭]
【发布时间】: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
  }
}

【问题讨论】:

    标签: swift swiftui uikit


    【解决方案1】:

    在这里引用Swift docs

    扩展为现有的类、结构、 枚举或协议类型。

    你可以使用下面的扩展,

    let image1 = UIImage(named: "yourimage1")
    let image2 = UIImage(named: "yourimage2")
    let finalImage = image1.mergeWith(image2)
    //Use finalImage for sharing or saving..
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多