0

I'm trying to grasp what is happening and I'm really confused by this. Basically, I have a UITableViewCell, and inside that cell I have a UIBUtton. When the table loads up and calls cellForRowAt, I set the UIButton image and add a target to it.

Before the app enters the background, I tap the UIButton which calls my function. In that function, I check that the image of the UIButton is equal to the image I set, which it is.

However, once the app enters the background and then re-enters the foreground, when I tap the UIButton and check whether the button's image is the same image that is set (and also visible) to me, it is not?

I dont understand why the UIButton image is not retained when the app enters the foreground after being in the background.

Here is some sample code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell

    let button = cell.viewWithTag(1) as! UIButton

    button(self, action: #selector(ViewController.checkImage), for: UIControlEvents.touchUpInside)

    button(#imageLiteral(resourceName: "button_stained"), for: UIControlState.normal)

}

fun checkImage(_ sender: UIButton)
{
    if sender.currentImage == #imageLiteral(resourceName: "button_stained")
    {
        print("TEST")
    }
}

TEST is only printed out initially when my view controller loads. If my app enters the background, then back to the foreground, TEST does not get printed out.

I have tried to also use sender.currentImage as well as sender.image(for: .normal) to no avail.

Can someone explain to me why this is happening?

Here is a sample project:

Test

Thanks.

Pangu
  • 3,721
  • 11
  • 53
  • 120
  • 1
    I don't think you can compare two `UIImage` objects using `==`, have a look at [this](http://stackoverflow.com/a/33937779/4539192) instead. – Rikh Apr 14 '17 at 10:17

2 Answers2

0

The problem is your "==" comparison. You have to use isEqual instead.

Take a look into the Apple API Reference section "Comparing Images".

ObjectAlchemist
  • 1,109
  • 1
  • 9
  • 18
  • issue is still not resolved with `isEqual`. I attached a sample project to test. – Pangu Apr 14 '17 at 14:50
  • Yes you are right. I think that iOS serialize the informations of the button when enter background. When enter foreground it deserialize the button. In this process the button creates a new instance of the image based on the same data. An image do not contain data, it's only an object that may create and cache data. Therefore direct comparison must fail. Normally isEqual handle exactly the difference between pointer and content comparison, maybe it's a bug in the apple framework.. – ObjectAlchemist Apr 14 '17 at 19:27
0

thanks to @Rikh for pointing me in the right direction

I was able to resolve the issue using the provided answer:

How to compare two UIImage objects

Updated for Swift 3.0:

extension UIImage
{
    func isEqualToImage(image: UIImage) -> Bool
    {
        let data1: Data = UIImagePNGRepresentation(self)!
        let data2: Data = UIImagePNGRepresentation(image)!
        return data1 == data2
    }
}
Community
  • 1
  • 1
Pangu
  • 3,721
  • 11
  • 53
  • 120