0

I am developing one application.In that i am doing comparision operation for two images like below

 UIImage *actual_image=usrcheck_image.image;
NSData *present_image = UIImagePNGRepresentation(actual_image);
NSData *compare_image = UIImagePNGRepresentation([UIImage imageNamed:@"unchk-1.png"]);

if([present_image isEqualToData:compare_image])
{
   set the checked image
}
else
{
   set the uncheck image.
 }

If i run this,check image is changed to uncheck.But uncheck image is not chneged to check image.Everytime else block is executed.So please help me how to compare these two images.This code is working perfectly in device.But the problem is in simulator only.

user1498119
  • 809
  • 1
  • 6
  • 8

4 Answers4

1

Try This Code -

Take 2 images of checked and unchecked and assign that images to UIButton.

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 160, 30);
[button setImage:[UIImage imageNamed:@"checkedImage.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"uncheckedImage.png"] forState:UIControlStateSelected];
button.tag = 1;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

And In buttonAction function -

selectedBtn.selected = NO;
button.selected = YES;
selectedBtn = button;

In .h file just mention

UIButton *selectedBtn; 

Hope It may help you.

iSmita
  • 1,292
  • 1
  • 9
  • 24
  • It's somewhat better.But i have to change the image for normal state not for selected state. – user1498119 Jun 27 '13 at 11:25
  • Ok.But may I know why do you need to perform image comparison? Let me know why it is needed so that I will try to solve your problem. :) – iSmita Jun 27 '13 at 12:24
0

You can use highlighted property of UIImageView. Set the checked image as highlightedImage property, and on the click event set:

yourImageView.isHighlighed = !yourImageView.isHighlighed;

it will automatically show your image as toggle

Prateek Prem
  • 1,544
  • 11
  • 14
  • @user1498119:Do you have issue with this ans – Prateek Prem Jun 27 '13 at 11:25
  • No use just a single image view. set it image property as unselected image. and set it highlighted property as selected image. and when you click on button than in tapped method use yourImageView.isHighlighed = !yourImageView.isHighlighed; this code only, nothing else you have to do more – Prateek Prem Jun 27 '13 at 12:18
0

May be link below can help you

Objective-C: Comparing an image against another image that has been previously saved

Let me know if this works

Community
  • 1
  • 1
Shashank Kulshrestha
  • 1,556
  • 17
  • 31
0

First thing is, there is no need of comparison of image Data and no need to compare images too,

But if you want to compare images means , just simply do like following

if(usrcheck_image.image==[UIImage imageNamed:@"unchk-1.png"]) {
    usrcheck_image.image=[UIImage imageNamed:@"chk-1.png"];
} else {
    usrcheck_image.image=[UIImage imageNamed:@"unchk-1.png"];
}
Venk
  • 5,949
  • 9
  • 41
  • 52