3

I am comparing cell.imageView.image with UIImage as below code.

Please refer below Code

    UIImage *imgOne = [UIImage imageNamed:@"One"];

    if([customCell.imageView.image isEqual: imgOne]){
          NSLog(@"Right Image");
    }

While debugging, I write as po [customCell.imageView.image isEqual imgOne], but it always returns as 'nil'.

Can anyone tell me how to compare this or proper way to compare?

halfer
  • 19,824
  • 17
  • 99
  • 186
Er.Shreyansh Shah
  • 1,482
  • 1
  • 9
  • 29

3 Answers3

5

Generally UIImageView can not store file name of image but it store only UIImage so, it is not possible to get file name of image which you add in UIImageView.

Thats way if you want to compare only image file name then it is not possible to compare it.

But If you want to compare two UIImage object then

UIImage *secondImage = [UIImage imageNamed:@"image.png"];

NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);

BOOL isCompare =  [imgData1 isEqualToData:imgData2];
if(isCompare)
{
  NSLog(@"Image View contains image.png");
}
else
{
  NSLog(@"Image View doesn't contains image.png");
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
3

If you matain the "image one" as a member var.(1)
And set the cell's image this value.(2)
Then you can compare using those two pointers.(3)
See this code:

//1
self.imgOne  = [UIImage imageNamed:@"One"];  
//2  
customCell.imageView.image=self.imgOne   
//3  
if(customCell.imageView.image == self.imgOne)
   {}
liaogang
  • 508
  • 3
  • 16
0

Instead of Imageview I tried with Button

And than i Check as below

    if([cell.button.currentImage isEqual: [UIImage imagenamed:"Your Imagenem"]]){
NSLog(@"Right Image");

}

It works fine..

Thank you.

Er.Shreyansh Shah
  • 1,482
  • 1
  • 9
  • 29