Rio de Janeiro, a city spectacularly forged between jungle and sea, can be many things to many people: a palm-fringed mecca for scantily clad pleasure seekers, the nerve center of Brazil’s oil industry, the cradle of musical genres ranging from samba to bossa nova and choro.

With his striking photographs, João Pina reminds us that Rio is also a theater of war.

The body count is still climbing in the labyrinth of Rio’s favelas, reflecting a devilishly complex struggle for control of the cocaine trade. Not far from Ipanema’s sands, drug gangs regularly wage gun battles not just with one another, but also with the police and paramilitary militias largely made up of active-duty police officers.

Photo

“What I’ve witnessed in Rio isn’t much different from what I’ve seen in places like Libya, Afghanistan or the Ivory Coast,” said Mr. Pina, 37, a Portuguese photographer who is no stranger to war zones.

If anything, Mr. Pina said, Rio is so awash in guns that it can sometimes make the firepower in other conflicts seem quaint by comparison. The title of his forthcoming book of photographs, “46570,” refers to the many thousands killed in the city in the decade leading up to the 2016 Summer Olympics.

The games were supposed to be a celebration of Brazil’s emergence as a powerhouse in the developing world and, for a while, the stars seemed be aligning for Latin America’s largest country. Brazil’s economy lifted off a decade ago, bolstered by deep-sea oil discoveries in the waters off Rio.

After decades of decline, a resurgent Rio fleetingly showcased Brazil’s global ambitions. Tanks rolled into favelas in a “pacification” program while work crews tore apart streets to expand transit systems. Skyscrapers and luxury hotels and lavish Olympic venues reconfigured the cityscape.

While all that was happening, Mr. Pina kept visiting the city and taking pictures. Rio is now mired yet again in graft scandals and surging violence; his photographs serve as a testament of how life remained a struggle during the boom years for many Cariocas, as the city’s residents are called.

Photo

Mr. Pina had already won acclaim for his work on the devastating legacy of Operation Condor, the campaign orchestrated in the 1970s by the intelligence agencies of right-wing South American military dictatorships to hunt down leftist dissidents in the region.

In Rio, Mr. Pina found a war largely devoid of ideology yet still producing casualties. One of the most sinister aspects of this bloodshed is how ingrained it is in the daily life of the city. It’s still possible to sip caipirinhas on a hotel terrace overlooking the Atlantic while gunmen slaughter one another in not-too-distant favelas.

Looking at Mr. Pina’s photographs brought back memories of how enchanting and unsettling Rio can feel. I arrived in the city as bureau chief for The Times in 2011, when the economy was sizzling and American professionals with a gold-rush mindset were descending on Rio to open hedge funds and technology start-ups.

Then I witnessed the erosion of Rio’s gains, venturing time and again into some of the same favelas where Mr. Pina was also doing his work, documenting evangelical Christian drug traffickers in prayer, bullet-ridden corpses and burials of police officers. I asked him about what it took to get so close to gun-wielding subjects.

Photo

“I believe the doors are more open to me since they know I’m not from Brazil and that my photographs are going to be published abroad,” said Mr. Pina, emphasizing how his upbringing in Portugal, the small European country that colonized Brazil, helped him in Rio.

“It’s the best of both worlds,” he explained, “not being a gringo in that I speak the same language, albeit with a crazy accent, but also not being Brazilian which would certainly prevent some of the access I’ve gained.”

I left Rio in 2017, when the city’s fortunes were at low ebb, but the calculating, vexing, sometimes comical negotiations for entry into Rio’s criminal underworld that Mr. Pina described still feel familiar.

Thankfully, Mr. Pina also cast his gaze on the splendor that mingles with misery on Rio’s streets. I could almost savor one of the tropical cloudbursts he photographed, remembering how heavy rains nurtured Rio’s urban jungle. Sometimes it takes an outsider to grasp the beauty and tragedy of a place.

Follow @viaSimonRomero and @nytimesphoto on Twitter. You can also find Lens on Facebook and Instagram.