Answered by Shaykh Amjad Rasheed
Is it permissible for a man to bring to mind images of women with the intention of doing away with sexual desire?
What Shaykh Ibn Hajar deemed reliable in the Tuhfa is that this is not forbidden. However, if [h: the one doing so] is determined that if able to, he will commit fornication with her, then [h: such thoughts are] forbidden.
That having been said, what is superior is for a Muslim to occupy his thoughts with acts of obedience and his tongue with the remembrance of Allah, for these two things are (if Allah wills) guaranteed to repel evil thoughts from him.
This is based on what Shaykh Ibn Hajar has related in the Tuhfa from Ibn al-Hajj al-Maliki that he related from some scholars who said that the like of what has been mentioned in the question is recommended and the one who does so will be rewarded, for he thereby guards his religion. He said, “One of our late scholars deemed this to be correct when one has a valid intention, meaning that one fears that one’s heart will become attached to her. He supported this position by what has been related in the rigorously authenticated hadith which commands the one who sees a woman he likes to have intercourse with his wife.” Ibn Hajar then goes on to say, “This position is problematic because getting addicted to such imagination will cause some attachment to the image [h: of the woman] to remain, so it incites attachment instead of doing away with it. The only thing that does away with it is to forget her appearance and thoughts of her, even if this is only done gradually, until eventually he becomes completely unattached to her.”
(Translated by Sidi Hamza Karamali)