iPhone,如何将一个图像叠加到另一个图像上以创建要保存的新图像?(水印)

Sol*_*444 26 iphone xcode overlay watermark uiimageview

基本上我想拍摄用户从照片库中选择的图像然后应用水印,右下角有一个应用程序名称的三角形.我已经在photoshop中使用透明层制作了第二张图像.

我尝试了一个功能,我不记得确切的名称,但它涉及CGIImages和掩码.这组合了两个图像,但作为一个遮罩,使得图像在透明层所在的位置更暗,而图像本身没有合并,只是掩盖了.

如何在不在屏幕上显示图像的情况下,将水印图像与其他图像合并,制作UIImage?

谢谢.

omz*_*omz 82

这很简单:

UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"];

UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

如果您希望背景和水印具有相同的大小,请使用此代码

...
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
...
Run Code Online (Sandbox Code Playgroud)

  • 工作得很完美!你是对的,这很容易.非常感谢你的帮助! (2认同)
  • 如果背景图像很小,那么水印显示非常大.请提供任何建议. (2认同)

Cod*_*key 11

omz提供的解决方案也适用于Swift,如下所示:

let backgroundImage = UIImage(named: "image.png")
let watermarkImage = UIImage(named: "watermark.png")

UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
backgroundImage.drawInRect(CGRect(0.0, 0.0, backgroundImage.size.width, backgroundImage.size.height))
watermarkImage.drawInRect(CGRect(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height))
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

30111 次

最近记录:

6 年,12 月 前