我需要调用 CGContextSaveGState 和 CGContextRestoreGState 吗?
Do I need to call CGContextSaveGState and CGContextRestoreGState?
仔细检查我是否需要调用 CGContextSaveGState 和 CGContextRestoreGState,即使是在创建新的 imageContext 时也是如此。
根据我在 Quartz 编程指南中阅读的内容,在下面的示例中我不需要这样做,因为我正在创建一个全新的图像上下文。
我搜索了 github UIGraphicsBeginImageContextWithOptions,一些人保存和恢复上下文,而其他人则不这样做。
// Uses a circularMask on an image
class func circularImage(image: UIImage, diameter: Int) -> UIImage {
assert(diameter > 0, "Diameter > 0 Failed \(__FUNCTION__)")
let frame = CGRect(x: 0.0, y: 0.0, width: Double(diameter), height: Double(diameter))
var newImage: UIImage?
UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.mainScreen().scale)
var context: CGContextRef = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
let imagePath: UIBezierPath = UIBezierPath(ovalInRect: frame)
imagePath.addClip()
image.drawInRect(frame)
newImage = UIGraphicsGetImageFromCurrentImageContext()
CGContextRestoreGState(context);
UIGraphicsEndImageContext()
return newImage!
}
不,你是对的,你不需要为你在那里做的事情保存上下文状态(你可以安全地注释掉你拥有的那些)。但是如果你想 'undo' 你所做的剪辑(例如)那么这是一种方法。小心始终保持这些 (save/restore)gstate 成对匹配,它是一个堆栈,所以你必须跟踪你放在那里的任何东西。
仔细检查我是否需要调用 CGContextSaveGState 和 CGContextRestoreGState,即使是在创建新的 imageContext 时也是如此。
根据我在 Quartz 编程指南中阅读的内容,在下面的示例中我不需要这样做,因为我正在创建一个全新的图像上下文。
我搜索了 github UIGraphicsBeginImageContextWithOptions,一些人保存和恢复上下文,而其他人则不这样做。
// Uses a circularMask on an image
class func circularImage(image: UIImage, diameter: Int) -> UIImage {
assert(diameter > 0, "Diameter > 0 Failed \(__FUNCTION__)")
let frame = CGRect(x: 0.0, y: 0.0, width: Double(diameter), height: Double(diameter))
var newImage: UIImage?
UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.mainScreen().scale)
var context: CGContextRef = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
let imagePath: UIBezierPath = UIBezierPath(ovalInRect: frame)
imagePath.addClip()
image.drawInRect(frame)
newImage = UIGraphicsGetImageFromCurrentImageContext()
CGContextRestoreGState(context);
UIGraphicsEndImageContext()
return newImage!
}
不,你是对的,你不需要为你在那里做的事情保存上下文状态(你可以安全地注释掉你拥有的那些)。但是如果你想 'undo' 你所做的剪辑(例如)那么这是一种方法。小心始终保持这些 (save/restore)gstate 成对匹配,它是一个堆栈,所以你必须跟踪你放在那里的任何东西。