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.
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.
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
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
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"];
}
Related
I have a custom UIButton like this:
_facebookButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_facebookButton addTarget:self
action:#selector(buttonFacebookPressed:)
forControlEvents:UIControlEventTouchDown];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
[_facebookButton setBackgroundImage:[UIImage imageNamed:#"facebookInactive"] forState:UIControlStateNormal];
} else {
[_facebookButton setBackgroundImage:[UIImage imageNamed:#"facebookInactive"] forState:UIControlStateNormal];
}
_facebookButton.frame = CGRectMake(0.0, 0.0, 33.0, 33.0);
_facebookButton.center = CGPointMake(60, 48);
_facebookButton.alpha = 0;
[_subView addSubview:_facebookButton];
It is in a UITableViewCell and on the simulator it looks OK, but on the actual device it is gone. I've searched the internet and they are always talking about image names which are not correct. I use the xcasset and have an image set named: facebookInactive.
Why is the button not showing up on the device?
Thanks in advance!
Are you sure about that,
ios Setting -> Facebook -> Sign in Process is completely registered ?
this may helpful to you.
Thanks & Regards,
Rushik Thumar.
You need to remove the statement
_facebookButton.alpha = 0;
or replace it with
_facebookButton.alpha = 1.0;
you can goto your project in Project Navigator then
select your Project -> Build Phases -> Copy Bundle resourses and check that their is ".xcasset" avilable.
if No then click on "+" icon & add ".xcasset"
Now build & Run.
something strange happened during the development of my app. I have a view with several UIButtons in it. All are using custom artwork and are UIButton with CostumType.
For me everything felt right. In the simulator and on the phone.
But when I give the app in someone else's hand the person taps on a button and it won't work. It feels like the button is just reaction to a certain tap which in fact doesn't feel right (if you compare it to normal behavior).
What can I do to make it behave normal? Normal means that somebody who is used to iOS Apps can use it like he except it works.
Here is an example code for a button:
focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2, 36, 40)];
focusButton.contentHorizontalAlignment = false; // I think these lines doesn't effect the behavior
focusButton.contentVerticalAlignment = false;
[focusButton setBackgroundImage:[UIImage imageNamed:#"arrowUp.png"] forState:UIControlStateNormal];
[focusButton setBackgroundImage:[UIImage imageNamed:#"arrowUpH.png"] forState:UIControlStateHighlighted];
[focusButton addTarget:self action:#selector(focusOrDefocusCourse) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:focusButton];
The Button Background looks like this:
this is woking fine.. Once just test with the frame..
UIButton *btn_foucs = [UIButton buttonWithType:UIButtonTypeCustom];
[btn_foucs setFrame:CGRectMake(20, 20, 200, 40)];
[btn_foucs setImage:[UIImage imageNamed:#"btn_erase.png"] forState:UIControlStateNormal];
[btn_foucs setImage:[UIImage imageNamed:#"btn_erase_h.png"] forState:UIControlStateHighlighted];
[self.view addSubview:btn_foucs];
You may like to take a look at the UIButton property imageEdgeInsets. This allows you to make the image draw into only part of the frame, so that the touchable area is bigger than the image itself (reducing the chance of 'missing' the button). You could do the following, for example:
int touch_offset = 10;
[focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108-touch_offset, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2-touch_offset, 36+(touch_offset*2), 40+(touch_offset*2))];
focusButton.imageEdgeInsets = UIEdgeInsetsMake(touch_offset, touch_offset, touch_offset, touch_offset);
This should make the touchable area 10 pixels wider than the image on each side, adjustable by changing the touch_offset value. As a general guideline, Apple recommend using touchable areas no smaller than 44x44 pixels.
I have created a custom UIButton for an iOS 4.3+ application. I wanted to make the background image stretch, so I and used the following methods to set its background image:
[myButton setBackgroundImage:[[UIImage imageNamed:myImagePath] stretchableImageWithLeftCapWidth:leftStretchValue topCapHeight:topStretchValue] forState:UIControlStateNormal]
[myButton setBackgroundImage:[[UIImage imageNamed:myHighlightedImagePath] stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateHighlighted]
[myButton setBackgroundImage:[[UIImage imageNamed:mySelectedImagePath]
stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateSelected]
I have added an event handler to the custom button
[myButton addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
In the above mentioned methods, I have set the selected and the highlighted state of the button accordingly. I have changed the frame of the UIButton to adjust the dimensions of an irregular image (mySelectedImage and myHighlightedImage, neither of which is a rectangle), so that it gets aligned properly along with myImage.
- (void) buttonSelected {
myButton.selected = !myButton.selected; //setting the selected property to NO in the viewDidLoad
//code to change the frame of the UIButton to accomodate the irregular image
}
- (void) buttonHighlighted {
myButton.highlighted = YES;
}
The problem I am facing is that when I select myButton, a gray color overlay is displayed on myImage for a very short time before the images myHighlightedImage and mySelectedImage are displayed as its background. I have set the background color of myButton as clearColor, thinking that might be the reason, but it is not getting solved.
What could be the possible issue... ?
button.adjustsImageWhenHighlighted = NO;
I think that is what you are looking for..
I want to check how to check if backgroundimage of UIbutton is present or not
I have code,
[Button setBackgroundImage:[UIImage imageNamed:#"default_module.png"] forState:UIControlStateNormal];
I want to check if there is image named "default_module.png" present and included in project folder or not.
Thanks
Just do this:
if(![UIImage imageNamed:#"default_module"]){
//The image was not present. Act accordingly.
}else{
//Continue as normal
}
I am creating an iphone app where I have used 25 buttons and each button has been shown a background image in its normal mode.I have also set another image on the highlighted mode of the button.
Initially button will be shown in its normal mode
now I want that when a button is pressed then button should change to its highlighted state so as the other image will be displayed there.
I have done so by doing:
button.highlighted = YES;
Its working but its sets the highlighted image for a fraction and then again normal state of the button come back. I use the following code to create the buttons.
for (int i=0; i<25; i++) {
if (i > 0) {
if (i%5 == 0) {
xaxis = 28;
yaxis = yaxis+42;
}
}
iconButton[i] = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton[i].frame = CGRectMake(xaxis, yaxis, 50, 40);
[iconButton[i] setBackgroundImage:[iconArray objectAtIndex:random] forState:UIControlStateNormal];
[iconButton[i] setBackgroundImage:[tapedIconArray objectAtIndex:random] forState:UIControlStateHighlighted];
[iconButton[i] addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:iconButton[i]];
xaxis = xaxis+53;
}
How can i hold the state of the button, I even tried with timer but app get crashed then.
Please help me
Many Thanks in advance
I am not sure of this but try it by setting image for button's UIControlStateSelected
i.e.
[btn setImage:[UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateSelected];