samedi 25 avril 2015

Do I need to call CGContextSaveGState and CGContextRestoreGState?


Double checking if I need to call CGContextSaveGState and CGContextRestoreGState even when creating a new imageContext.

From what I've read in the Quartz Programming Guide, I shouldn't need to in the example below, because the I'm creating a brand new image context.

I've searched github for UIGraphicsBeginImageContextWithOptions and some folks save and restore the context and other folks do not.

// 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!
}


Aucun commentaire:

Enregistrer un commentaire