如何合并两个UIImages? [英] How to merge two UIImages?
问题描述
我正在尝试合并两个不同的图像并创建一个新图像。这是我想要的方式:
我有这个图像(A):
I am trying to merge two different images and create a new one. This is the way I would like to do: I have this image (A):
这是一张PNG图片,我想合并这个与另一个图像(B),我从手机上创建这样的东西:
It's a PNG image and I would like to merge this one with another image (B) which I took from the phone to create something like this:
我需要合并A的功能B创建C.尺寸必须保留在A图像中,图像B应自动调整尺寸以适应宝丽来(A)。有可能吗?感谢您的帮助!
I need a function who merge A with B creating C. The size must remain from the A image and the image B should auto adapt the size to fit into the polaroid (A). Is it possible to do that? Thank for your help!
更新
只需一件事,图像(A)是一个正方形,我拍摄的图像是一个16:9,我怎么能解决这个问题?如果我使用你的功能,我拍摄的图像(B)会变得拉长!
UPDATE Just one thing, the image (A) is a square and the image i took is a 16:9, how can i fix that?? If i use your function the image (B) that i took become stretched!
推荐答案
希望这可以帮到你,
var bottomImage = UIImage(named: "bottom.png")
var topImage = UIImage(named: "top.png")
var size = CGSize(width: 300, height: 300)
UIGraphicsBeginImageContext(size)
let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
bottomImage!.draw(in: areaSize)
topImage!.draw(in: areaSize, blendMode: .normal, alpha: 0.8)
var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
所有最佳:)
这篇关于如何合并两个UIImages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!