I've ran into the same problem with my 24 inch iMac but come to find out the problem was with my video card. After the card started getting hot it would shut down my iMac . this is pretty common with iMac MacBook pros laptops there's two ways to fix it. One replace the video card which can be expensive or two reset the video chip on your video card . That will require you to bake the video card for about eight minutes at 350° if you're not technically inclined with breaking your Mac down I would not attempt it.