首页 > 解决方案 > CGRect 在 Swift 中改变原点

问题描述

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

我想像图片一样更改“原点”的位置以构成水印。

贝娄是我的代码。

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

我应该如何更改我的代码?

标签: iosswiftcgrect

解决方案


如果要将水印放在右下角,请使用:

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

...

在这种情况下更改原点是可能的,但不切实际。


推荐阅读