swiftyboi
swiftyboi

Reputation: 3221

How can I add a watermark to an image using this code?

I know there are several other ways to do this; I don't want to import anything that I don't need to. If someone can help me with his code, that would be great.

Currently, it is only saving the original image without the watermark image.

extension UIImage {

    class func imageWithWatermark(image1: UIImageView, image2: UIImageView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(image1.bounds.size, false, 0.0)
        image2.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        image1.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}

func addWatermark() {
    let newImage = UIImage.imageWithWatermark(imageView, image2: watermarkImageView)
    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
}

EDIT: I've got the watermark appearing on the saved images.

I had to switch the order of the layers:

 image1.layer.renderInContext(UIGraphicsGetCurrentContext()!)
 image2.layer.renderInContext(UIGraphicsGetCurrentContext()!)

HOWEVER, it is not appearing in the correct place.It seems to always appear in the center of the image.

Upvotes: 8

Views: 11972

Answers (4)

iTarek
iTarek

Reputation: 776

    static func addWatermartToImage(image: UIImage, watermarkName: String?) -> UIImage {
    if let watermarkName, let watermarkImage = UIImage(named: watermarkName) {
        // Calculate target width as 20% of the image width
        let targetWidth = image.size.width * 0.2
        
        // Calculate scale factor to maintain aspect ratio
        let scaleFactor = targetWidth / watermarkImage.size.width
        
        // Calculate new watermark height maintaining aspect ratio
        let newWatermarkHeight = watermarkImage.size.height * scaleFactor
        
        // Configure padding and positioning
        let padding: CGFloat = image.size.width * 0.02 // 2% padding
        
        // Position in bottom right corner
        let watermarkRect = CGRect(
            x: image.size.width - targetWidth - padding,
            y: image.size.height - newWatermarkHeight - padding,
            width: targetWidth,
            height: newWatermarkHeight
        )
        
        let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        
        // Create graphics context with proper scale
        UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale)
        
        // Draw the main image
        image.draw(in: imageRect, blendMode: .normal, alpha: 1)
         
         // Draw the watermark with the specified color
         if let cgImage = watermarkImage.cgImage {
             let coloredWatermark = UIImage(cgImage: cgImage, scale: watermarkImage.scale, orientation: watermarkImage.imageOrientation)
             coloredWatermark.draw(in: watermarkRect, blendMode: .lighten, alpha: 0.8)
         }
        
        let result = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return result
    } else {
        return image
    }
}

Upvotes: 0

Gerges Eid
Gerges Eid

Reputation: 1075

The answer of @mrkbxt in

Swift 5:

func addWatermartToImage (image: UIImage?) {
    if let img = image, let watermarkImg = UIImage(named: "watermark.png") {

        let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)

        UIGraphicsBeginImageContextWithOptions(img.size, true, 0)
        let context = UIGraphicsGetCurrentContext()

        context!.setFillColor(UIColor.white.cgColor)
        context!.fill(rect)

        img.draw(in: rect, blendMode: .normal, alpha: 1)
        watermarkImg.draw(in: CGRect(x: x, y: y, width: width, height: height), blendMode: .normal, alpha: 0.6)

        let result = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil)

    }
}

Upvotes: 0

Sachin Rasane
Sachin Rasane

Reputation: 1559

SWIFT 4 Use this

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

let size = backgroundImage.size
let scale = backgroundImage.scale

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

let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Use result to UIImageView, tested.

Upvotes: 9

mrkbxt
mrkbxt

Reputation: 491

If you grab the UIImageViews' images you could use the following concept:

if let img = UIImage(named: "image.png"), img2 = UIImage(named: "watermark.png") {

    let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)

    UIGraphicsBeginImageContextWithOptions(img.size, true, 0)
    let context = UIGraphicsGetCurrentContext()

    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextFillRect(context, rect)

    img.drawInRect(rect, blendMode: .Normal, alpha: 1)
    img2.drawInRect(CGRectMake(x,y,width,height), blendMode: .Normal, alpha: 1)

    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil)

}

Upvotes: 16

Related Questions