Gamma-ray coded-aperture imaging technology plays an important role in nuclear security, decommissioning of nuclear facilities, and nuclear medicine diagnosis. However, under near-field imaging condition, artifacts in the reconstructed image can interfere with identifying the shape and position of the radioactive source. In this paper, a gamma-ray coded-aperture imaging method based on mask and anti-mask functions was proposed to suppress imaging artifacts and speed up the acquisition of low-noise reconstructed images. Through simulation, the effects of the number of iterations and the thickness of the coded-aperture collimator on the imaging quality were studied, and the range of the optimal correction factor in the method was determined. Imaging experiments were conducted using a compact coded-aperture gamma camera based on CdZnTe detector to verify the applicability of the optimal correction factor range. The limitations of the proposed method were analyzed through complex-shaped source imaging simulations and multi-source imaging experiments. This method has an insufficient suppression effect on random artifacts and requires further improvement in imaging irregular radioactive sources. However, it has good imaging performance for single-point source and multi-point sources, effectively reducing regular cross-shaped and stripe-like artifacts. In the non-uniform radioactive background, it can eliminate a part of artifacts, significantly improving imaging quality. Therefore, this method has potential applications in complex radioactive environments.