LampShade
LampShade

Reputation: 2775

How to dispose of a UIButton's Image Contents only, not the button itself

Say I have a UIButton and I gave it a background image, like so:

UIButton button = new (....);
button.SetBackroundImage = UIImage.FromFile(....);

How do I dispose of the buttons image without holding onto the image variable? I tried the below code but it doesn't appear to work.

button.CurrentBackgroundImage.Dispose() 

My case here, is that I have a collectionview with 10 visible items but could have hundreds of things in it. Obviously it's only reusing 10 cells with 10 uibuttons. The images getting drawn and set as the background image of the UIButtons are drawn dynamically and need to be disposed of once you scroll away and reload the re-usable collection view cell

Upvotes: 1

Views: 218

Answers (2)

Milen
Milen

Reputation: 8867

Using IDisposable pattern in C#:

using (var image = UIImage.FromFile ("yourImageFile.png")){
    button.SetBackroundImage = image;
}

Upvotes: 0

Mark McCorkle
Mark McCorkle

Reputation: 9414

Just set the image to nil. iOS will dispose of the image when necessary.

[button setImage:nil forState:UIControlStateNormal];

For Xamarin specific Dispose usage check here.

Upvotes: 0

Related Questions