fillky
fillky

Reputation: 581

One UIImage instance for multiple UIImageViews as backgrounds

I have multiple UIImageViews in my application hierarchy and I need to set one UIImage for all of them with ability to change image for all UIImageViews at once. So I created UIImage as singleton pattern and then I set this image instance for each UIImageView. The problem is, that when a change the singleton image the UIImageViews are holding old image and not that newly assigned. You can image the situation like changing background for multiple views or screens and ability to change that background at once only by changing that UIImage instance. Why aren't UIImageViews updated? I don't know well about drawing images in UIImageView but I need to understand this behaviour. And I don't want to add new image to every screen and then changing all of them to achieve new "background effect". Thanks

Upvotes: 2

Views: 463

Answers (2)

squarefrog
squarefrog

Reputation: 4822

You probably want to create a method that updates all of your UIImageView instances at once. For example:

- (void)updateImage:(UIImage *)image { self.imageViewA.image = image; self.imageViewB.image = image; self.imageViewC.image = image; self.imageViewD.image = image; self.imageViewE.image = image; self.imageViewF.image = image; self.imageViewG.image = image; self.imageViewH.image = image; }

If you are just creating a background pattern, I do suggest you follow Davide's method.

================

Edit: following on from the discussion in the comments, you could achieve this more succinctly using:

- (void)updateImage:(UIImage *)image { for (UIImageView *subview in self.viewArray) { subview.image = image; } }

Upvotes: 0

DeFrenZ
DeFrenZ

Reputation: 2252

If you want all the UIImageView in your application to apply the new background you can do something like [[UIImageView appearance] setBackgroundColor:[UIColor colorWithPatternImage:yourImage]]

(this is not a really proper solution as UIImageView usually should use its image property and not the backgroundColor one)

Upvotes: 1

Related Questions