In my answer to this question I included a whole passage of a textbook as an image in order to save me the time to write up this part myself. Is this ok (as long as I give a reference to the source, of course) or are there some legal issues?
Yes, this is OK. Try to use blockquote formatting (select the text and use the quotation mark button on the editing toolbar) when quoting text from a book. In this case you've used an image, so you don't need to blockquote.
Stack Exchange is protected as a DMCA safe harbor, so even if copyright violations are posted, neither Stack Exchange is not liable (you may be, but till date I haven't seen a case where that has happened). If the copyright holder wishes, he can easily submit a DMCA takedown request to SE, and they will remove the relevant content.
While it's not against the rules to quote from books, it's always a good idea to have a good amount of "wrapper" material that you've written to go around the quote. This is to better tailor the answer to the question (a quote rarely exactly addresses a question), and also to make the answer less dependent on the quote. Even before the huge edit, your answer had a nice amount of wrapper text :)
So you're fine. Carry on :)