Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
bkSwifty Avatar asked Jan 29 '16 18:01

bkSwifty


People also ask

How do I make an image a watermark in HTML?

Another way is to create a relative div and put it in the flow of the page, placing the main image first and then place the watermark on top. Or you could overlay your image with watermark text. This can easily be done with HTML or CSS.

How do you add a watermark in HTML?

You should apply opacity: . 6; for example and position absolute the div. For watermarks you can probably go for div's with some styling and position it to be fixed in the place you like.

How do you put a watermark on a picture?

Install the Add Watermark on Photos (free, in-app purchases available) app from the Play Store. Launch the app, and tap Apply on Images. Select the photo you want to add a watermark to and tap Done. Tap Create Watermark to add a text watermark, or tap Select from Gallery to add an image as a watermark.

How do I add a watermark to an image in Python?

For adding watermark to our image, we need “Image”, “ImageDraw” and “ImageFont” modules from pillow package. The 'ImageDraw' module adds functionality to draw 2D graphics onto new or existing images. The 'ImageFont' module is employed for loading bitmap, TrueType and OpenType font files.


1 Answers

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)

}
like image 126
mrkbxt Avatar answered Sep 21 '22 07:09

mrkbxt