You might have more luck asking this question in /r/etymology, but etymonline (based off the OED) gives this explanation of the word "pig" used as a term of derision:
Applied to persons, usually in contempt, since 1540s; the derogatory slang meaning "police officer" has been in underworld slang at least since 1811.
It then quotes from the Dictionary of Buckish Slang, University Wit and Pickpocket Eloquence published in London in 1811:
"The pigs frisked my panney, and nailed my screws; the officers searched my house, and seized my picklock keys."
In fact, in that actual book, the 1811 dictionary very explicitly defines the slang "pig" as "police officer".
A later 1874 slang dictionary published in London perhaps gives some further insight into the origin of the term:
Pig, a policeman, an informer. The word is now almost exclusively applied by London thieves to a plain-clothes man, or a "nose".
And then under "nose", that dictionary gives the definitions:
Nose, a thief who turns informer; a paid spy; generally called a policeman's nose; "on the nose", on the lookout.
Nose, to give information to the police, to turn approver.
It's interesting to note that the first modern police force--that is, a hired patrol to keep order and look out for crime day and night--is usually said to date to the Marine Police in London beginning in 1798, who patrolled the port of London. The Metropolitan Police evolved out of that, beginning with the passage of the Metropolitan Police Act of 1829.
Before that, law enforcement was carried out by constables, magistrates, and justices of the peace, who were either appointed or sometimes elected (depending on when and where we're talking about), but weren't a 24/7 patrol force. If you were the victim of a crime, you would seek out your local constable, tell him what happened, and he'd go and arrest the guy. Or, in smaller jurisdictions, or in less serious crimes, you would show up at the local courthouse on the next "court day", typically held once a week or once a month. There, you'd tell the local justices of the peace what had happened, and then they'd subpoena the accused to show up on the next court day to respond to the charges.
When not traced back to the Marine Police, then the beginning of modern police forces is usually traced back to the Bow Street Runners, a group hired in 1749 by magistrates in London's Bow Street to do investigations. Though, unlike the Marine Police, they weren't initially patrolmen, but more detectives, and stand-ins for the magistrates and constables with the power to arrest people. The 1811 slang dictionary even gives one of the definitions for "pig" as "a Bow-street officer".
So, it's interesting that the term "pig" was already being applied to police officers within a couple decades of the formation of modern police patrol forces, and within the first ~sixty years of the formation of the Bow Street Runners detective force. "Pig" even seems to predate the slang terms "cop", "copper", and "bobby".
This journal article gives a brief overview of how the English magistry worked in the 18th and early 19th Centuries before modern police forces, and briefly touches on why there ended up being a push to replace them with trained police, citing an 1829 pamphlet entitled "Observations upon the Institution of Unpaid Justices of the Peace" as one such piece of literature that helped make that push. A few years later, The Law Magazine published an article entitled "Unpaid Justices of the Peace" which also cited the earlier pamphlet when detailing the issues with the way law enforcement was being carried out in the UK at that time. (TL;DR: Appointed, unpaid court officers were often ineffective and could be biased when trying to seek justice against accused criminals.)
Earlier, in 1755, one of the founders of the Bow Street Runners had published a similar booklet detailing A Plan for Preventing Robberies Within Twenty Miles of London: With an Account of the Rise and Establishment of the Real Thieftakers.
A recent history of the Bow Street Runners has been published by the Oxford University Press under the title The First English Detectives: The Bow Street Runners and the Policing of London, 1750-1840, though a search for the word "pig" doesn't turn up any results.
For what it's worth, London's Metropolitan Police has an official history of itself that explains how and why they came into being, though I doubt there, either, that it goes into any detail on how the police earned the nickname "pig".
To answer your question more succinctly, it's a bit ambiguous why the slang term "pig" came into use to describe police officers, though we do know that it happened early in the history of the formation of modern police forces. It could be that it was just a general derogatory term meaning an "offensive person" that had already been in use for centuries, and then it came to specifically describe the police by London criminals. Or it could be an allusion to early police officers and detectives who were sniffing out crime, as a pig sniffs with its snout, since a related slang term had police informants nicknamed "noses". But the evidence points to it being one of those two, or a combination of both.