如何合并两个UIImage?

我试图合并两个不同的图像,并创build一个新的。 这是我想要做的方式:我有这样的形象(A): 偏光

这是一个PNG图像,我想将这个与另一个图像(B)合并,我从手机中拿出来创build这样的图像: 宝丽来合并

我需要一个将A与B合并的function.C尺寸必须保留在A图像上,而图像B应该自动调整尺寸以适合宝丽来(A)。 有没有可能做到这一点? 感谢您的帮助!

更新只是一件事,图像(A)是一个正方形,我采取的图像是16:9,我该如何解决? 如果我使用你的function,我所拍摄的图像(B)变得拉长了!

希望这可以帮助你,

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!.drawInRect(areaSize) topImage!.drawInRect(areaSize, blendMode: kCGBlendModeNormal, alpha: 0.8) var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() 

祝一切顺利 :)

这样覆盖图片将会更清洁

 class func mergeImages(imageView: UIImageView) -> UIImage { UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0) imageView.superview!.layer.renderInContext(UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } 

在这里输入图像说明

目标C版本的这个解决scheme与顶部图像重新集中的逻辑:

 -(UIImage *)getImageInclosedWithinAnotherImage { float innerImageSize = 20; UIImage *finalImage; UIImage *outerImage = [UIImage imageNamed:@"OuterImage.png"]; UIImage *innerImage = [UIImage imageNamed:@"InnerImage.png"]; CGSize outerImageSize = CGSizeMake(40, 40); // Provide custom size or size of your actual image UIGraphicsBeginImageContext(outerImageSize); //calculate areaSize for re-centered inner image CGRect areSize = CGRectMake(((outerImageSize.width/2) - (innerImageSize/2)), ((outerImageSize.width/2) - (innerImageSize/2)), innerImageSize, innerImageSize); [outerImage drawInRect:CGRectMake(0, 0, outerImageSize.width, outerImageSize.height)]; [innerImage drawInRect:areSize blendMode:kCGBlendModeNormal alpha:1.0]; finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return finalImage; 

}