Core Graphics Vs Images for custom button

When should I use the main graphics above the images to create a custom UIButton? Is the graphical shell faster? Besides independence from resolution, are there other key benefits?

+6
source share
2 answers

Benefits of basic graphics:

  • The code for drawing the button is likely to be smaller than the image file.
  • Allows dynamic change, small changes without adding a full second image.
  • As you mentioned, permission is not dependent.
  • Less memory (does not allocate memory to store each pixel).

Pluses of images:

  • Creating an image in an editor will usually be easier than writing code that draws great. (If you use anything other than solid colors, it could be a lot easier.)
  • The editor will allow you to see the image in advance without recompiling.
  • It’s easier to work with other built-in objects (i.e. you can do this with a UIButton background).

As for work time, I'm not sure. I would suggest that CG will be faster for simple drawing, where most of the pixels will not be changed, but images will be faster for more complex drawing, where most of the pixels will be changed (Assuming you use PNG format, so it becomes wholesale Otherwise CG will probably always be faster).

As a compromise, you can draw the image once and use this image for future drawing. This will allow you to get some benefits from each.

+11
source

Additional thoughts in a comment by ughoavgfhw :

  • Images can be cached (for example, when using [UIImage imageNamed:] ). Thus, you will not even use more memory for new buttons except the first. (And no allocations except memory for the new pointer).

  • You can use stretch images to create buttons and avoid some (not all and not always) resolution dependency problems:

     UIImage *myImg = [UIImage imageNamed:@"myImg.png"]; UIImage *myImgStretchable = [myImg stretchableImageWithLeftCapWidth:10 topCapHeight:10]; [myButton setBackgroundImage:myImgStretchable forState:UIControlStateNormal]; 
+5
source

Source: https://habr.com/ru/post/889839/


All Articles