1
別の透明な画像を選択して別の画像に追加する方法を教えてください。
クラウンノーズ、帽子、帽子、イヤリング、ヒゲ、眼鏡などの透明な写真
これはいくつかのアプリケーションに存在しますが、これについてのスウィフトサンプルは見つかりません。スイフト:イメージを別のイメージに追加するにはどうしたらいいですか?
ありがとうございました。
別の透明な画像を選択して別の画像に追加する方法を教えてください。
クラウンノーズ、帽子、帽子、イヤリング、ヒゲ、眼鏡などの透明な写真
これはいくつかのアプリケーションに存在しますが、これについてのスウィフトサンプルは見つかりません。スイフト:イメージを別のイメージに追加するにはどうしたらいいですか?
ありがとうございました。
私はUIImageの拡張子に素敵な機能を持っている:
extension UIImage {
static func imageByMergingImages(topImage: UIImage, bottomImage: UIImage, scaleForTop: CGFloat = 1.0) -> UIImage {
let size = bottomImage.size
let container = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
UIGraphicsGetCurrentContext()!.interpolationQuality = .high
bottomImage.draw(in: container)
let topWidth = size.width/scaleForTop
let topHeight = size.height/scaleForTop
let topX = (size.width/2.0) - (topWidth/2.0)
let topY = (size.height/2.0) - (topHeight/2.0)
topImage.draw(in: CGRect(x: topX, y: topY, width: topWidth, height: topHeight), blendMode: .normal, alpha: 1.0)
return UIGraphicsGetImageFromCurrentImageContext()!
}
}
ですから、このような方法で呼び出すことができます。
let image = UIImage.imageByMergingImages(topImage: top, bottomImage: bottom)
あなたの特定のケースでは、あなたは多くのオーバーレイを超える追加することを考慮
extension UIImage {
func imageOverlayingImages(_ images: [UIImage], scalingBy factors: [CGFloat]? = nil) -> UIImage {
let size = self.size
let container = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
UIGraphicsGetCurrentContext()!.interpolationQuality = .high
self.draw(in: container)
let scaleFactors = factors ?? [CGFloat](repeating: 1.0, count: images.count)
for (image, scaleFactor) in zip(images, scaleFactors) {
let topWidth = size.width/scaleFactor
let topHeight = size.height/scaleFactor
let topX = (size.width/2.0) - (topWidth/2.0)
let topY = (size.height/2.0) - (topHeight/2.0)
image.draw(in: CGRect(x: topX, y: topY, width: topWidth, height: topHeight), blendMode: .normal, alpha: 1.0)
}
return UIGraphicsGetImageFromCurrentImageContext()!
}
}
次に、最終画像iを作成することができますこの方法で:
var imageClownFace = UIImage(named: "clown_face")!
imageClownFace = imageClownFace.imageOverlayingImages([imageNose, imageHat, imageCap])
私は周り遊ぶことができる作業サンプルプロジェクトファイルがありますか?私はSWIFTで本当に新しく、サンプルをコンパイルするのに苦労しています。ありがとう。 – greg