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

3

8

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.

Eryn answered 29/1, 2016 at 18:6 Comment(5)
so you want to simply put some text on a image ?Adulation
did u look into #6993330 ?Adulation
Sorry, no an image. I want an image on top of another image.Eryn
Why dont you try to place an imageview on imageview and then snapshot it ?Adulation
I'll try that. Maybe I've been overthinking it. Haha thanks!Eryn
A
9

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.

Aphesis answered 18/12, 2018 at 11:31 Comment(0)
R
16

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)

}
Relly answered 29/1, 2016 at 20:40 Comment(0)
A
9

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.

Aphesis answered 18/12, 2018 at 11:31 Comment(0)
I
0

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)

    }
}
Intracutaneous answered 24/11, 2023 at 3:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.