As a metal-free photocatalyst, graphitic carbon nitride (g-CN) shows great potential for photocatalytic water splitting, although its performance is significantly limited by structural defects due to incomplete polymerization. In the present work, we successfully synthesize highly conjugated g-CN nanofoam through an iodide substitution technique. The product possesses a high polymerization degree, low defect density, and large specific surface area; as a result, it achieves a hydrogen evolution rate of 9.06 mmol h-1 g-1 under visible light irradiation, with an apparent quantum efficiency (AQE) of 18.9% at 420 nm. Experimental analysis and theoretical calculations demonstrate that the recombination of photogenerated carriers at C-NHx defects was effectively depressed in the nanofoam, giving rise to the high photocatalytic activity.