The Iwo Jima and Okinawa invasions were planned as part of the same campaign. The goal was the airfields within 500 miles of the mainland. Our capture of Guam, Saipan, the Marianas and Tinian gave us long-range heavy bomber capability to carpet bomb Japanese factories, but the B-24s and B-29s had a rough time of it. It was a long flight: 3000 miles round trip, about 16 hours. Tokyo was right at the edge of the B-29s range, so a lot of planes made it back on fumes, or, if they took shrapnel through a fuel line, ditched in the middle of nowhere*. Plus, when they got over Japan, they had to contend with the Japanese air force all by themselves. They had no fighter escorts - the longest range U.S. fighter, the P47-N, only had a range of about 1800 miles (that's with drop tanks). The bomber wings were taking pretty high casualties, losing about 5% of all bombers sent out on each sortie, plus lots of dead gunners. We needed to have airfields much closer so we could have emergency landing places for the B-29s, SAR aircraft for downed crews, and those lovely fighter escorts. Air superiority wins wars, so the U.S. needed to capture islands much closer to the mainland.
Iwo Jima jumped off first (19 February – 26 March 1945). Iwo Jima had three airfields, but it's only 8 square miles, not a lot of room, and no deep anchorage ports for the Navy. Okinawa (1 April – 21 June 1945) had four existing airfields, plus it's 500 sq. mi. Bonus: it's in the Sea of Japan. This allowed us to attack from two directions and also support some of the Allied forces in China.
I don't think we realized how costly it was going to be to take the island. Plus we really REALLY wanted/needed those airfields. We thought we had figured out how to deal with the Japanese tunnel problem: flame tanks (M4A3R3 Zippo). We captured two of the four airbases within hours, and within a couple of days we held half of the island. It was taking the rest that was the problem.
Another aspect to this is the whole world was getting weird while this was going on. We invaded Okinawa Island on April 1. USSR entered the war against Japan on April 6. President Roosevelt died on April 12. Ernie Pyle, the famous and well-loved war correspondent, was killed on April 18th while accompanying a mop-up operation on one of the outlying Okinawan islands. Germany surrendered on May 8, and we hoped Japan might be demoralized and surrender as well. That didn't happen. We needed to end the war, fast. Momentum was on our side, morale was on our side. We had a new, 'unknown' president (Truman). The U.S. was running out of creative ways to raise money. Churchill, Stalin and Roosevelt had already met in Yalta to carve up post-war Germany. The world was changing, a new order was arising. We were already looking beyond, and realized that we needed to be done with this whole thing.
* I highly recommend "Unbroken" by Laura Hillenbrand. True story about a former U.S. Olympic runner who became a B-24 bombardier. Their plane developed serious mechanical problems on a long-range search mission. They ditched in the middle of the ocean, survived in a life raft for 47 days, then were captured by the Japanese and sent to one of the worst POW camps till the end of the war. Amazing story.