The gunman who ambushed and murdered two NYPD officers as they sat in their squad car Saturday afternoon in Brooklyn vowed to kill police in a number of social media posts that vowed retaliation for the deaths of Michael Brown and Eric Garner, authorities said.

Ismaaiyl Brinsley, 28, wrote on his Instagram account: "I'm putting wings on pigs today. They take 1 of ours, let's take 2 of theirs," hours before the killings, two city officials with direct knowledge of the case confirmed for The Associated Press. He used the hashtags Shootthepolice RIPErivGardner (sic) RIPMikeBrown. The post also included an image of a silver handgun and the message, "This may be my final post. The post had more than 200 likes but also had many others admonishing his statements.

Police said Brinsley, who was black, approached the passenger window of a marked police car at approximately 2:45 p.m. local time and opened fire, striking Officers Rafael Ramos and Wenjian Liu in the head. The officers -- one Hispanic, one Asian -- were on special patrol doing crime reduction work in the Bedford-Stuyvesant section of Brooklyn.

"They were, quite simply, assassinated -- targeted for their uniform," said Police Commissioner Bill Bratton during a news conference at Woodhull Medical Center, where the officers were pronounced dead Saturday evening.

"Our city is in mourning. Our hearts are heavy," said Mayor Bill de Blasio. "It is an attack on all of us."

Scores of officers in uniform lined up three rows deep at the hospital driveway. The line stretched into the street. Officers raised their hands in a silent salute as two ambulances bore away the slain officers' bodies. The mayor ordered flags at half-staff.

Brinsley fled to a nearby subway station, where he shot himself in the head as a subway train door full of people closed. A silver handgun was recovered at the scene, Bratton said. The New York Post reported that the recovered gun matches the one in the Instagram picture.

Two unnamed Con Edison workers tried to stop Brinsley before he entered the station, but retreated.

When the workers confronted Brinsley on the street, he pointed his gun at him and asked them, “You want some of this?” before they backed off, the New York Post reports.

The two workers alerted police to Brinsley’s location.

“That’s how the cops knew he was in there,” one law enforcement source told the New York Post.

A second Instagram post dated just after the shooting showed the same camouflage pants and distinctive blue sneakers worn by the gunman as his body was carried from the scene on a stretcher.

Late Saturday, authorities in Baltimore County, Md. said that Brimsley had shot and wounded his ex-girlfriend at an apartment complex in Owings Mills early in the morning before traveling to New York.

Officials identified the woman Sunday as 29-year-old Shaneka Nicole Thompson. Authorities say Thompson was shot in the abdomen by Brinsley and remains in critical but stable condition.

Authorities say she is expected to survive her injuries and that they hope to interview her later Sunday if her condition improves. Police believe that Thompson and Brinsley had a previous romantic relationship.

The Washington Post reported that authorities in Maryland became aware of the threatening Instagram posts by Brimsley at around 1:30 p.m. local time. They were able to trace the posts and Brimsley's phone to a location in Brooklyn and contacted the NYPD's 70th Precinct to alert them that he was in the area. At the same time, authorities faxed a "Wanted" poster to the NYPD with more information about Brimsley. Around the time of the shooting, another message with the same information was sent to New York's "real-time crime center."

"The tragedy here was that just as the warning was coming in, the murder was occurring," Bratton said Saturday evening.

Brinsley had a history of arrests in Georgia for robbery, disorderly conduct and carrying a concealed weapon. Bratton said his last-known address was in Georgia, but he had some ties to Brooklyn.

Ramos was married with a 13-year-old son and had another in college, police and a friend told the Associated Press. He had been on the job since 2012 and was a school safety officer. Liu had been on the job for seven years and got married two months ago.

Rosie Orengo, a friend of Ramos, said he was heavily involved in their church and encouraged others in their marriages.

"He was an amazing man. He was the best father and husband and friend," she said. "Our peace is knowing that he's OK, and we'll see him in heaven."

The shooting comes at a time when police in New York and nationwide have been criticized by some over the circumstances surrounding the death of Garner, who was stopped by police for selling loose, untaxed cigarettes. Amateur video captured an officer wrapping his arm around Garner's neck in what some have described as a chokehold and wrestling him to the ground. Garner was heard saying, "I can't breathe" and later died.

Demonstrators around the country have held protests since a grand jury decided on Dec. 3 not to indict the officer involved in Garner's death, a decision that closely followed a Missouri grand jury's decision not to indict Officer Darren Wilson in the fatal shooting of Brown, 18.

Several New York officers were assaulted during demonstrations, including one event that drew thousands to the Brooklyn Bridge and at which two lieutenants were attacked.

The families of Garner and Brown condemned Saturday’s shootings.

"My husband was not a violent man, so we do not want any violence connected to his name," Esaw Garner said Sunday.

A statement released Saturday by the Parks & Crump law firm says Brown's family rejects "any kind of violence directed toward members of law enforcement. It cannot be tolerated."

It also calls on citizens to "work together to bring peace to our communities."

The president of the police officers union, Patrick Lynch, and De Blasio have been locked in a public battle over treatment of officers following the decision not to indict the officer in Garner's death. Just days ago, Lynch suggested police officers sign a petition that demanded the mayor not attend their funerals should they die on the job. De Blasio was also criticized for not speaking out about the two lieutenants who were assaulted in the protest at the Brooklyn Bridge.

"Had Mayor Bill de Blasio been forceful from the onset when the two lieutenants were attacked, one has to question as to whether this murderous psycho would have been compelled to target our heroic brother and sister in New York's Finest," Jon Adler, National President of the Federal Law Enforcement Officers Association told Fox News in a statement.

"That blood on the hands starts at the steps of City Hall, in the office of the mayor," Lynch said late Saturday. "After the funerals, those responsible will be called on the carpet and held accountable."

The last shooting death of an NYPD officer came in December 2011, when 22-year veteran Peter Figoski responded to a report of a break-in at a Brooklyn apartment. He was shot in the face and killed by one of the suspects hiding in a side room when officers arrived. The triggerman, Lamont Pride, was convicted of murder and sentenced in 2013 to 45 years to life in prison.

Fox News' Lucas Tomlinson and The Associated Press contributed to this report.