There are places were you cannot place a float as figure, like a minipage, but you want a figure with numbered caption. Then have sense the second option.
Otherwise use the first method even if you do not want moving figures. Instead, use the [h] option, or even [H] of package `float. The reason is that floats also take care of spaces above and below the caption, with another floats and the surrounding text, and you can change that setting consistently in the preamble.
While the second approach in the next MWE show two problems:
(1) The risk to end in another page, as pointed Ulrike Fisher
(2) The lack space between the caption and the following text.
\documentclass{article}
\usepackage{caption}
\usepackage{lipsum}
\usepackage{graphicx}
\begin{document}
\lipsum[1]
\begin{figure}[h]
\centering
\includegraphics[scale=.2]{example-image}
\caption{my caption here}
\label{my label here}
\end{figure}
\lipsum[2-7][1-38] % wrong result 2: caption in another page
\begin{center}
\includegraphics[scale=.2]{example-image}
\end{center}
\captionof{figure}{my caption here}
\label{my label here}
\lipsum[3] % wrong result: No space under caption
\end{document}
With a middle paragraph shorter, you will note still a third problem: a different space above the caption. As you as you can see by the comments, this is however a bad use of \captionof, causing the warning "outside box or environment", so if you use instead as option B:
\begin{center}
\includegraphics[scale=.2]{example-image}
\captionof{figure}{my caption here}
\end{center}
This way you avoid any warning, and the excessive space between the image and the caption, but note, this is still not equivalent to option A:
\begin{figure}[h]
\centering
\includegraphics[scale=.2]{example-image}
\caption{my caption here}
\end{figure}
Make now a MWE with only A+A+A+B+B+B+B. You can note that:
1) Center is still a breakable environment (in last B).
2) The space under each float (A) is bigger (and look better) that under the caption of each center environment (B).
One option to solve (1) is, as pointed several times, is use a minipage:
Option C:
\noindent\begin{minipage}{\linewidth}
\centering
\includegraphics[scale=.2]{example-image}
\captionof{figure}{my caption here}
\end{minipage}
But compare now with A+A+A+C+C+C+C: The small space after the captions of minipages is really ugly. But in a real document it could be similar or higher than in floats, depending of \parskip settings. You should also pay attention to \parindent and where is the minipage whitin the paragraph. Just after a blank line, the minipage start a paragraph, and it will be indented, changing the apparent center of the figure, whereas after another minipage will be in the second line of paragrah, thus without indentation.
On the other side, floats never are breakables, and never will be indented, and the spacing of floats will be independent of \parskip, but controlled by \textfloatsep, \floatsep and \intextsep (see article defaults here).
However, consider that a non-float box have some other inherent problems.
1) It cannot move from the desired position. Yes, maybe what you want, but what happens if it is too large for the space still available on the page? The figure pass to the next page leaving all this space empty. With a float with [h], the figure can pass to the top of next page equally (is changed to if needed[ht] automatically) , but filling that space with following text. The document layout will be incomparably better, and if you use labels for make a references in text, this displacement is a minor inconvenient.
2) The float rules have not effect in a non-float (obviously). Floats rules prevent you of use, for instance, put four big figures on top of the page so you can only add 4 or five lines of text at the bottom. But a non-float figure disregard \topfraction and topnumer counter or totalnumber counter, so no one will avoid that horrible layout. This rules can be changed, or even ignored (e.g., [h!]) and how important they are depend of what type of document are doing, but for a book or a thesis the document, most probably their layout will look better with that rules.
If the problem is only that the float is too mobile, you should read first How to influence the position of float environments like figure and table in LaTeX?, then use the h option (e.g.: \begin{figure}[h] ...), or even h!, and if it is still not immobile enough, add the package float and change that option to H. My advice is, however, avoid these options (if fact, all float options) and consider only that in the final draft. First check if worth change the default floats positions, the tune float rules (for instance, allowing one or two more floats per page) and finnally set the options for some float if needed.
Considering all the above, a figure environment is a priori is the preferable choice, imho, and do not need any package, whereas \captionof in a minipage it's a good life saver when you cannot use a float, like using the multicol package.
frameby default is not defined (actually it would clash with the\framecommand and give errors. Did you meanfigure? It is usually best to usefigurebut note the only function of thefigureenvironment is to allow the content to be moved and re-inserted to avoid bad white space at page breaks. So moving to a new position is the expected behaviour not a problem to be solved. – David Carlisle Dec 11 '19 at 08:05frameinstead offigurein the first option. I just editted it tofigure. – user203101 Dec 11 '19 at 08:08figureandtablein LaTeX? – Werner Dec 11 '19 at 11:57\captionofis misplaced in the second example, it should be inside the center environment (and probably inside a minipage) – David Carlisle Dec 12 '19 at 01:19