Search code examples
iosswiftcgrect

CGRect change origin in Swift


https://drive.google.com/file/d/1Nd6q9OmDppJrxjZIRODzffVDiBXKnB_e/view?usp=sharing

I'd like to change the position of the 'origin' like a picture for compose the watermark.

Bellow is my code.

var watermark = UIImage(named: "logo.png")
var newHeight = filteredImage.image?.size.height
var newWidth = filteredImage.image?.size.width

UIGraphicsBeginImageContext(CGSize(width:newWidth!, height:newHeight!))

watermark.draw(in: CGRect(x:-0, y:-0, width:400, height:300))

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

watermark = newImage!

filter = CIFilter(name: "CISourceOverCompositing")
filter!.setValue(CIImage(image: watermark), forKey: "inputImage")
filter!.setValue(coreImage1, forKey: "inputBackgroundImage")

How should I change my code?


Solution

  • If you want to place watermark to right bottom corner, use:

    var watermark = UIImage(named: "logo.png")!
    var newSize = filteredImage.image.size
    
    UIGraphicsBeginImageContext(newSize)
    
    watermark.draw(in: CGRect(
        x: newSize.width - watermark.image.width,
        y: newSize.height - watermark.image.height,
        width: watermark.image.width,
        height: watermark.image.height
    ))
    
    ...
    

    Changing the origin is possible but impractical in this case.