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

时间:2011-08-11 02:10:42

标签: iphone xcode uiimageview overlay watermark

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

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

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

谢谢。

5 个答案:

答案 0 :(得分:81)

这很简单:

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)];
...

答案 1 :(得分: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()

答案 2 :(得分: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;

}

答案 3 :(得分:1)

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

将结果用于UIImageView,经过测试。

答案 4 :(得分: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!
    }