【问题标题】:iPhone, how does one overlay one image onto another to create a new image to save? (watermark)iPhone,如何将一张图像叠加到另一张图像上以创建要保存的新图像? (水印)
【发布时间】:2011-08-11 02:10:41
【问题描述】:

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

我尝试了一个函数,我不记得确切的名称,但它涉及 CGIImages 和掩码。这结合了两个图像,但是作为一个遮罩,这使得透明层所在的图像更暗,图像本身并没有合并,只是被遮罩了。

如何让水印图像与另一个图像合并,制作 UIImage,而不在屏幕上显示图像?

谢谢。

【问题讨论】:

标签: iphone xcode uiimageview overlay watermark


【解决方案1】:

这很简单:

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

【讨论】:

  • 完美运行!你是对的,这很容易。非常感谢您的帮助!
  • 最好使用 UIGraphicsBeginImageContextWithOptions 而不是 UIGraphicsBeginImageContext 来保持视网膜显示器上的图像质量。请参阅stackoverflow.com/questions/4334233/… 了解更多信息。
  • 你先生值得蛋糕!这是一个很好的解决方案。我也有同样的需求,你为我节省了很多时间。
  • 为什么这不能保留视网膜图像分辨率?任何建议如何保留它?
  • 如果背景图像很小,则水印显示很大。请提供任何建议。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    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】:

      你可以使用这个方法,非常动态,你可以指定第二张图片的起始位置和图片的总大小。

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

      【讨论】:

        【解决方案5】:

        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!
            }
        

        【讨论】:

          猜你喜欢
          • 2011-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-03
          • 2013-03-22
          • 1970-01-01
          相关资源
          最近更新 更多