iPhone,如何叠加一张图片到另一张上创建一个新的图片并保存?(水印)

28

基本上,我想要从用户的照片库中选择一张图片,然后应用水印,在右下角放置一个三角形,上面写着应用程序名称。我已经在Photoshop中用透明层制作了第二张图片。

我尝试了一个函数,我记不清确切的名字了,但它涉及到CGIImages和掩模。这将两个图像合并为一个掩模图像,使得透明层的区域变暗,图像并没有真正地合并,只是被掩蔽。

我该如何将水印图像与另一张图像合并,生成一个UIImage,而不显示在屏幕上呢?

谢谢。


可能是重复问题:在iPhone上从其他两个UIImages创建UIImage - Rob Napier
5个回答

83

很简单:

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();

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

...
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
...

2
完美地工作了!你说得对,那很容易。非常感谢你的帮助! - SolidSnake4444
最好使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext来保留Retina显示屏上的图像质量。请参阅https://dev59.com/s2855IYBdhLWcg3wcz_y获取更多信息。 - Jan Soltis
您真是应得一块蛋糕!这是一个非常出色的解决方案。我有同样的需求,您为我节省了很多时间。 - Jordan
为什么这不能保留视网膜图像分辨率?有什么建议可以保留它吗? - stackOverFlew
这是如何保留视网膜分辨率的方法:https://dev59.com/s2855IYBdhLWcg3wcz_y - stackOverFlew
2
如果背景图片很小,那么水印会显示得非常大。请提供任何建议。 - Jagdev Sendhav

13

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

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

UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height))
watermarkImage.draw(in: CGRect(x: backgroundImage.size.width - watermarkImage.size.width, y: backgroundImage.size.height - watermarkImage.size.height, width: watermarkImage.size.width, height: watermarkImage.size.height))
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

2

SWIFT 4

let backgroundImage = imageData!
let watermarkImage = #imageLiteral(resourceName: "jodi_url_icon")

UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height))
watermarkImage.draw(in: CGRect(x: 10, y: 10, width: watermarkImage.size.width, height: backgroundImage.size.height - 40))

let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.imgaeView.image = result

使用result作为UIImageView的参数,已测试。


1
您可以使用这种方法,非常灵活,可以指定第二个图像的起始位置和图像的总大小。
-(UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width{

    CGSize size = CGSizeMake(width,40);
    UIGraphicsBeginImageContext(size);

    CGPoint pointImg1 = CGPointMake(0,0);
    [img drawAtPoint:pointImg1];

    CGPoint pointImg2 = cropRect.origin;
    [img2 drawAtPoint: pointImg2];

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;

}

1

SWIFT 5 函数:

func addWaterMark(image: UIImage) -> UIImage {
        let backgroundImage = image//UIImage(named: "image.png")
        let watermarkImage = UIImage(named: "waterMark.png")

        UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
        backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height))
        watermarkImage!.draw(in: CGRect(x: backgroundImage.size.width - watermarkImage!.size.width, y: backgroundImage.size.height - watermarkImage!.size.height, width: watermarkImage!.size.width, height: watermarkImage!.size.height))
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result!
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接