[英]Cropping a transparent hole in an UIImage
我想在UIImage中創建一個洞,但它必須是一個透明的洞。 整個將被放置在精靈的中間,但為了測試,我在左上角創建了一個整體:
我完成了這個:
let hole = CGRect(x: 0, y: 0, width: 512, height: 512)
let context = UIGraphicsGetCurrentContext()!
context.addRect(hole)
context.clip(using: .evenOdd)
context.setFillColor(UIColor.white.cgColor)
context.fill(hole)
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
我不完全了解如何使用UIGraphicsGetCurrentContext類。 我不明白上下文是什么? 或者我為什么要同時使用addRect()和fillRect()? 或者是什么片段?
然而,雖然這些問題很重要,但我主要擔心的是我創造的洞不透明:它是白色的。 我嘗試通過這樣做來修復它:
context.setFillColor(UIColor.clear.cgColor)
然而,這並沒有造成任何漏洞。 你們有什么建議我做的?
嘗試使用context.clear(CGRect)
如文檔中所定義
/* Clear `rect' (that is, set the region within the rect to transparent). */ @available(iOS 2.0, *) public func clear(_ rect: CGRect)
這就是你的代碼應該如何
let hole = CGRect(x: 0, y: 0, width: 512, height: 512)
let context = UIGraphicsGetCurrentContext()!
context.clear(hole)
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
希望這對你有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.