         context.scaleBy(x: 1.0, y: -1.0)
         
         context.draw(cgImage, in: CGRect(x: -size.width * 0.5, y: -size.height * 0.5, width: size.width, height: size.height))
-        let image = UIGraphicsGetImageFromCurrentImageContext()
-        UIGraphicsEndImageContext()
-        
-        return image
+        return UIGraphicsGetImageFromCurrentImageContext()
     }
     
     private func _flip(horizontally: Bool) -> UIImage! {
         let rect = CGRect(origin: .zero, size: size)
-        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
+        UIGraphicsBeginImageContextWithOptions(rect.size, false, scale)
+        defer { UIGraphicsEndImageContext() }
         guard let cgImage = self.cgImage, let context = UIGraphicsGetCurrentContext() else { return nil }
         context.clip(to: rect)
         if horizontally {

    /// Creates and render a snapshot of the view hierarchy into the current context. Returns nil if the snapshot is missing image data, an image object if the snapshot is complete.
    public var contents: UIImage! {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, UIScreen.main.scale)
        if #available(iOS 7.0, *) {
            if !drawHierarchy(in: bounds, afterScreenUpdates: false) {
                guard let context = UIGraphicsGetCurrentContext() else { return nil }

            
            let e = vImageBuffer_InitWithCGImage(&effectInBuffer, &format, nil, input, vImage_Flags(kvImagePrintDiagnosticsToConsole))
            if e != kvImageNoError {
                return nil
            }
            
