Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between UIImageView.hidden and UIImageView.image = nil

In my view, I have multiple views (UICollectionViewCells) that, depending on the model, can include an UIImageView as a subview (each with a separate instance).

For my case, the views that don't show the UIImageView outnumber the ones that do show it.

I can choose to either call UIImageView.hidden = false when I want the views to show the image, or to set the image inside the image view i.e. UIImageView.image = UIImage(named: ...).

I'm wondering, which is the more performant approach, with memory and speed concerns? I have a feeling that the difference is not significant enough, especially with UIImage(named:)'s caching, but I do want to find out.

like image 644
peco Avatar asked Aug 29 '16 07:08

peco


1 Answers

  1. if you set UIImageView.image = nil, then surely if an image is in memory, it will be released (then reallocated if reused), so I suggest always to do it

  2. if you want to be sure that UIImageView.image (1) will not be visible, (2) does not occupy a frame in the cell and (3) will not imply rendering time, then also set UIImageView.hidden = true

I suggest to take both actions. Performances are not a problem here, in my opinion (considering also that you have very few cells with image inside)

like image 57
ddb Avatar answered Sep 23 '22 16:09

ddb